mmap64.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* mmap - map files or devices into memory. Linux version.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <sys/mman.h>
  19. #include <sysdep.h>
  20. #include <mmap_internal.h>
  21. /* To avoid silent truncation of offset when using mmap2, do not accept
  22. offset larger than 1 << (page_shift + off_t bits). For archictures with
  23. 32 bits off_t and page size of 4096 it would be 1^44. */
  24. #define MMAP_OFF_HIGH_MASK \
  25. ((-(MMAP2_PAGE_UNIT << 1) << (8 * sizeof (off_t) - 1)))
  26. #define MMAP_OFF_MASK (MMAP_OFF_HIGH_MASK | MMAP_OFF_LOW_MASK)
  27. /* An architecture may override this. */
  28. #ifndef MMAP_PREPARE
  29. # define MMAP_PREPARE(addr, len, prot, flags, fd, offset)
  30. #endif
  31. void *
  32. __mmap64 (void *addr, size_t len, int prot, int flags, int fd, off64_t offset)
  33. {
  34. MMAP_CHECK_PAGE_UNIT ();
  35. if (offset & MMAP_OFF_MASK)
  36. return (void *) INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
  37. MMAP_PREPARE (addr, len, prot, flags, fd, offset);
  38. #ifdef __NR_mmap2
  39. return (void *) MMAP_CALL (mmap2, addr, len, prot, flags, fd,
  40. (off_t) (offset / MMAP2_PAGE_UNIT));
  41. #else
  42. return (void *) MMAP_CALL (mmap, addr, len, prot, flags, fd, offset);
  43. #endif
  44. }
  45. weak_alias (__mmap64, mmap64)
  46. libc_hidden_def (__mmap64)
  47. #ifdef __OFF_T_MATCHES_OFF64_T
  48. weak_alias (__mmap64, mmap)
  49. weak_alias (__mmap64, __mmap)
  50. libc_hidden_def (__mmap)
  51. #endif