1 # Example showing mmap syscall.
 2 # Create a new segment using mmap, save the address, write to it.
 3 #
 4 # To run (from the subx directory):
 5 #   $ subx translate examples/ex12.subx -o examples/ex12
 6 #   $ subx run examples/ex12
 7 # You shouldn't get a segmentation fault.
 8 
 9 == code
10 #   instruction                     effective address                                                   register    displacement    immediate
11 # . op          subop               mod             rm32          base        index         scale       r32
12 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
13 
14     # syscall(mmap, 0x1000)
15     bb/copy-to-EBX  Mmap-new-segment/imm32
16     b8/copy-to-EAX  0x5a/imm32/mmap
17     cd/syscall  0x80/imm8
18 
19     # write to *EAX to check that we have access to the newly-allocated segment
20     c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0x34/imm32              # copy to *EAX
21 
22     # syscall(exit, EAX)
23     89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                       # copy EAX to EBX
24     b8/copy-to-EAX  1/imm32/exit
25     cd/syscall  0x80/imm8
26 
27 == data
28 
29 # various constants used here were found in the Linux sources (search for file mman-common.h)
30 Mmap-new-segment:  # type mmap_arg_struct
31     # addr
32     00 00 00 00  # null
33     # len
34     00 01 00 00  # 0x1000
35     # protection flags
36     03 00 00 00  # PROT_READ | PROT_WRITE
37     # sharing flags
38     22 00 00 00  # MAP_PRIVATE | MAP_ANONYMOUS
39     # fd
40     ff ff ff ff  # -1 since MAP_ANONYMOUS is specified
41     # offset
42     00 00 00 00  # 0 since MAP_ANONYMOUS is specified
43 
44 # . . vim:nowrap:textwidth=0