%include "SYS.macros" global start section .text start: ;; Tell the user what's going on SYS_WRITE 1,msg1,msg1len SYS_OPEN maps,O_RDONLY,0x0 ; okay, let's see if we can read our own /proc/self/maps file... mov [mapsfd],eax ; save file descriptor in [mapsfd] SYS_READ [mapsfd],somedata,4096 ; I hope that open(2) did leave a useful file descriptor in eax SYS_WRITE 1,somedata,eax SYS_CLOSE [mapsfd] ;; Establish a heap SYS_BRK 0 add eax,1000 SYS_BRK eax ;; Tell the user SYS_WRITE 1,msg2,msg2len SYS_OPEN maps,O_RDONLY,0x0 ; okay, let's see if we can read our own /proc/self/maps file... mov [mapsfd],eax ; save file descriptor in [mapsfd] SYS_READ [mapsfd],somedata,4096 ; I hope that open(2) did leave a useful file descriptor in eax SYS_WRITE 1,somedata,eax SYS_CLOSE [mapsfd] ;; Create a mmap SYS_MMAP2 0,1024,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_ANONYMOUS|MAP_PRIVATE,-1,0 mov [mmapaddr],eax ; preserve the return for later... ;; Tell the user SYS_WRITE 1,msg3,msg3len SYS_OPEN maps,O_RDONLY,0x0 ; okay, let's see if we can read our own /proc/self/maps file... mov [mapsfd],eax ; save file descriptor in [mapsfd] SYS_READ [mapsfd],somedata,4096 ; I hope that open(2) did leave a useful file descriptor in eax SYS_WRITE 1,somedata,eax SYS_CLOSE [mapsfd] ;; Create a second mmap. Have to actually specify an address some distance away, or ;; the kernel will just extend the first mmap-ing mov ebx,[mmapaddr] sub ebx,4096*10 SYS_MMAP2 ebx,1024,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_ANONYMOUS|MAP_PRIVATE,-1,0 ;; Tell the user SYS_WRITE 1,msg4,msg4len SYS_OPEN maps,O_RDONLY,0x0 ; okay, let's see if we can read our own /proc/self/maps file... mov [mapsfd],eax ; save file descriptor in [mapsfd] SYS_READ [mapsfd],somedata,4096 ; I hope that open(2) did leave a useful file descriptor in eax SYS_WRITE 1,somedata,eax SYS_CLOSE [mapsfd] SYS_EXIT 0 section .data ;; Strings first maps: db '/proc/self/maps',0 mapsfd: dd 0 msg1: db 'This program shows the normal development of a Linux process. First we see a text segment, a data segment',10 db 'a stack segment, and a vdso.',10,10 db 'Here is your initial memory mapping from /proc/self/maps -- please note that there is no heap',10 db 'yet:',10,10 msg1len equ $ - msg1 msg2: db 10,'Now we will create a heap by calling brk(2) twice.',10,10 db 'Okay, that is done. Let us look at our new map:',10,10 msg2len equ $ - msg2 msg3: db 10,10,'Now we use mmap2(2) to create yet another segment, an anonymous memory mapping:',10,10 msg3len equ $ - msg3 msg4: db 10,10,'Now we use mmap2(2) to create a second anonymous memory mapping segment:',10,10 msg4len equ $ - msg4 ;; A bit of space to save our first mmap's base address mmapaddr: dd 0 ;; Finally, a buffer for /proc/self/maps data somedata: times 4096 db 0