1/* mmap - map files or devices into memory. Linux version.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <unistd.h>
21#include <sys/mman.h>
22#include <sysdep.h>
23#include <mmap_internal.h>
24
25#ifdef __NR_mmap2
26/* To avoid silent truncation of offset when using mmap2, do not accept
27 offset larger than 1 << (page_shift + off_t bits). For archictures with
28 32 bits off_t and page size of 4096 it would be 1^44. */
29# define MMAP_OFF_HIGH_MASK \
30 ((-(MMAP2_PAGE_UNIT << 1) << (8 * sizeof (off_t) - 1)))
31#else
32/* Some ABIs might use __NR_mmap while having sizeof (off_t) smaller than
33 sizeof (off64_t) (currently only MIPS64n32). For this case just set
34 zero the higher bits so mmap with large offset does not fail. */
35# define MMAP_OFF_HIGH_MASK 0x0
36#endif
37
38#define MMAP_OFF_MASK (MMAP_OFF_HIGH_MASK | MMAP_OFF_LOW_MASK)
39
40/* An architecture may override this. */
41#ifndef MMAP_PREPARE
42# define MMAP_PREPARE(addr, len, prot, flags, fd, offset)
43#endif
44
45void *
46__mmap64 (void *addr, size_t len, int prot, int flags, int fd, off64_t offset)
47{
48 MMAP_CHECK_PAGE_UNIT ();
49
50 if (offset & MMAP_OFF_MASK)
51 return (void *) INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
52
53 MMAP_PREPARE (addr, len, prot, flags, fd, offset);
54#ifdef __NR_mmap2
55 return (void *) MMAP_CALL (mmap2, addr, len, prot, flags, fd,
56 (off_t) (offset / MMAP2_PAGE_UNIT));
57#else
58 return (void *) MMAP_CALL (mmap, addr, len, prot, flags, fd, offset);
59#endif
60}
61weak_alias (__mmap64, mmap64)
62libc_hidden_def (__mmap64)
63
64#ifdef __OFF_T_MATCHES_OFF64_T
65weak_alias (__mmap64, mmap)
66weak_alias (__mmap64, __mmap)
67libc_hidden_def (__mmap)
68#endif
69

source code of glibc/sysdeps/unix/sysv/linux/mmap64.c