Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
", syscall(9,    0, 10, 3, 2 | 32, -1, 0));
printf("mmap-2: %lli
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...