I was wondering where I could find mmap flag values on os x. The manpages for mmap say to use MAP_PRIVATE, MAP_... and such, but if you are dealing with assembly you have to know the actual values to make the syscall. I tried looking for the header files that defined these constants but I could not find it. Could someone link it possibly?
-
1A link to somewhere outside SO is just a dead link waiting to happen, so is discouraged. And the values may be operating system dependent so you really need to just find them. Since the compiler can find them on your computer, you can too. – old_timer Jul 27 '16 at 02:33
-
1if you are aiming for using assembly, then just disassemble some C code that makes these calls and see what values the compiler is generating, if you can write assembly you can read it, true? – old_timer Jul 27 '16 at 02:34
-
@dwelch Yeah you are right. – k3v Jul 27 '16 at 02:38
-
find the include directory and try grep -r MAP_PRIVATE * see what you see... – old_timer Jul 27 '16 at 02:59
-
2you can use gcc --help ton find gcc -print-search-dirs and that may give you an idea where to start looking for headers. On linux you dont need that go to /usr/include grep for map private finds it instantly #define MAP_PRIVATE 0x02 then go to that file that has that define and probably find more. – old_timer Jul 27 '16 at 03:03
-
@dwelch Thanks a lot for your help! I found the file. I didn't know everything would be in usr/include. "sys/mann.h" – k3v Jul 27 '16 at 03:07
-
Note that is where it is on your operating system. It is elsewhere on mine, same define for that value but elswhere. Thus the grep. I like Alden's answer very much too, I didnt know about that gcc feature, now I do. – old_timer Jul 27 '16 at 12:15
2 Answers
To find the right file in the first place: two options:
- google the macro, and look at the documentation to see what headers it says to include. e.g. the
mmap
man page only mentionssys/mman.h
. - Or: learn where your system keeps header files, and recursive-grep there. (Or use
ack
, because it knows not to search binary files and is generally good for this sort of thing). e.g. on my GNU/Linux system,
ack --hh MAP_PRIVATE /usr/include/ /usr/lib/gcc/x86_64-linux-gnu/5/
will search only.h
files (--hh
) under those two directories, which is where all the system headers live.
To find where your system keeps headers in the first place, cpp output (without -dM
) includes current-line-number setting lines which tell you which headers got included. e.g.
# 216 "/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h" 3 4
Use the C preprocessor to dump macro values.
gcc -E -dM
prints only #define
lines with the final values of all macros at the end of processing, instead of the preprocessed source. -
tells it to read from stdin, so you can pipe a code fragment into it with echo
.
echo '#include <sys/mman.h>' | gcc -E - -dM | less
In less
, you can of course search with /
. You can also filter with &
, hiding lines that don't match the regex. So you can type &MAP_ to get just the MAP_ macro definitions.
On x86-64 GNU/Linux, I get:
#define MAP_32BIT 0x40
#define MAP_TYPE 0x0f
#define MAP_EXECUTABLE 0x01000
#define MAP_FAILED ((void *) -1)
#define MAP_PRIVATE 0x02
...
Maybe using this in a build system instead of hard-coding your search results
If you're writing some actual asm code that wants these macro definitions, remember that gcc -c foo.S
runs .S
files through the C preprocessor before assembling. However, sys/mman.h
contains lines other than macro definitions (like typedef unsigned char __u_char;
) which aren't valid asm syntax.
The most future-proof / portable way to do this might be for your build scripts / Makefile to use gcc -E -dM
to create a local macro-only version of the system headers you need. Then your .S
files could #include "system_macros/mman.h"
and so on.
However, note the ((void *) -1)
definition for MAP_FAILED
: that syntax won't assemble, so this doesn't always work for writing portable asm that works on different systems with the same ABI but different constants and syscall numbers.
For syscall numbers, the system asm/unistd.h
usually only has macros, not prototypes or typedefs.
Your asm source should look like this:
# 4th arg (flags) goes in r10 for the syscall ABI, vs. rcx for function calls
mov $(MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB), %r10d
Or for NASM, use something like sed
to turn the cpp macros into NASM .equ
definitions:
mov r10d, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB

- 328,167
- 45
- 605
- 847
Using the -E
option with gcc allows you to see the output of a source file after the preprocessor. Using gcc -E test.c
on the following source file
#include <sys/mman.h>
int main() {
return MAP_PRIVATE;
}
outputs
...
# 2 "asdf.c" 2
int main() {
return 0x02;
}
which shows that MAP_PRIVATE
is equal to 0x02
on my system. This may not be true on all systems, however.

- 2,229
- 1
- 15
- 21
-
You can just `printf '%s\n' '#include
' MAP_PRIVATE | gcc -E - | tail` or something. Or just `-dM` to dump macro defs. See also my answer – Peter Cordes Jul 27 '16 at 05:25 -
Note that this is not a gcc option, `-E` is a standard POSIX option every C compiler should have. – fuz Jul 27 '16 at 05:55