Looking for mmap flag values

蹲街弑〆低调 提交于 2019-12-08 05:11:35

问题


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?


回答1:


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:


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 mentions sys/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


来源:https://stackoverflow.com/questions/38602525/looking-for-mmap-flag-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!