mmap2(2)

πŸ’– mmap2 β€” map files or devices into memory πŸ’–

okay bestie, settle in, because we're reading the mmap2(2) page from the System Calls Manual. yes, section 2, the syscalls section, we're being ✨feral✨ today.

🌈 LIBRARY

Standard C library (libc, -lc) β€” the usual queen.

πŸ“Ž SYNOPSIS

#include <sys/mman.h>    /* Definition of MAP_* and PROT_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>

void *syscall(SYS_mmap2, unsigned long addr, unsigned long length,
              unsigned long prot, unsigned long flags,
              unsigned long fd, unsigned long pgoffset);

🫢 DESCRIPTION

okay so real talk before anything else: this is probably not the system call that you are interested in πŸ’€ go look at mmap(2) instead, because that's where the glibc wrapper function lives, and that wrapper is the one actually calling this system call. iykyk.

so what does mmap2() even DO. it gives you the exact same interface as mmap(2), no cap, except the final argument doesn't count the offset into the file in bytes like mmap(2) does β€” it counts it in 4096-byte units. slay. and why do we care? because this is what lets applications that are stuck using a 32-bit off_t still map large files, up to 2^44 bytes. iconic behavior honestly.

⭐ RETURN VALUE

  • on success, mmap2() hands you back a pointer to the mapped area πŸŽ€
  • on error, it returns -1 and errno gets set to tell you what went wrong. rude but informative.

😭 ERRORS

  • EFAULT β€” there was a problem with getting the data from user space. not me, not you, just the data. πŸ™ˆ
  • EINVAL β€” (various platforms where the page size is not 4096 bytes.) offset * 4096 is not a multiple of the system page size. the math just did NOT math on those platforms bestie.
  • and honestly? mmap2() can also return any of the errors described in mmap(2). so go read that too.

πŸ¦‹ VERSIONS

  • on architectures where this system call is present, the glibc mmap() wrapper function invokes this system call rather than the mmap(2) system call. she's the understudy that's actually on stage every night.
  • this system call does not exist on x86-64. periodt. it's simply not invited to that party.
  • on ia64, the unit for offset is actually the system page size, rather than 4096 bytes. plot twist πŸ’€ different unit, same energy.

πŸ’… STANDARDS

Linux. that's it, that's the whole vibe.

πŸ‘‘ HISTORY

Linux 2.3.31. a legend from the archives.

🌸 SEE ALSO

getpagesize(2), mmap(2), mremap(2), msync(2), shm_open(3)


and that's the whole page bestie 🫢 this little masterpiece came to you from Linux man-pages 6.20, dated 2026-02-08, starring mmap2(2) in the leading role πŸ’–βœ¨ go read mmap(2) now fr fr 🌈

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms