Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x32_mmap.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Test program which explores whether mmap's ofs parameter
  2. // is 64-bit, and whether it needs to be shifted << PAGE_SHIFT.
  3. // Apparently it is 64-bit and isn't shifted.
  4. //
  5. // Build: x86_64-gcc -static -Wall -ox32_mmap x32_mmap.c
  6. // Typical output:
  7. // 7f9390696000-7f93906a6000 r--s 12345670000 08:06 2224545 /etc/passwd
  8. // ^^^^^^^^^^^
  9. #define _GNU_SOURCE
  10. #include <sys/types.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <asm/unistd.h>
  19. // Ensure we are compiling to 64 bits
  20. struct bug { int t[sizeof(long) > 4 ? 1 : -1]; };
  21. int main(int argc, char **argv)
  22. {
  23. long ofs = 0x12345670000; // fails if not page-aligned
  24. errno = 0;
  25. close(0);
  26. if (open("/etc/passwd", O_RDONLY))
  27. return 1;
  28. long r = syscall(
  29. (long) (__NR_mmap | 0x40000000), // make x32 call
  30. (long) (0), // start
  31. (long) (0x10000), // len
  32. (long) (PROT_READ), // prot
  33. (long) (MAP_SHARED), // flags
  34. (long) (0), // fd
  35. (long) (ofs) // ofs
  36. );
  37. printf("ret:0x%lx errno:%m\n", r);
  38. char buf[16*1024];
  39. sprintf(buf, "/proc/%d/maps", getpid());
  40. int fd = open(buf, O_RDONLY);
  41. if (fd > 0) {
  42. int sz = read(fd, buf, sizeof(buf));
  43. if (sz > 0)
  44. write(1, buf, sz);
  45. }
  46. return 0;
  47. }