Hello,
I begin to program with NASM under FreeBSD (11.1-RELEASE FreeBSD 11.1-RELEASE #0 r321309).
I use the C lib to get basic functions like console output. To do that, I link with gcc:
My problem is that I can't make mmap() working. Here is the simplest code:
The answer of mmap() in rax is always -1, which means MAP_FAILED.
I also tried a syscall:
Here, the return code is 9: Bad file descriptor.
I think it's something obvious but I'm stuck. Can someone help me?
I begin to program with NASM under FreeBSD (11.1-RELEASE FreeBSD 11.1-RELEASE #0 r321309).
I use the C lib to get basic functions like console output. To do that, I link with gcc:
nasm -f elf64 prog.asm && gcc -o prog prog.o -lc
My problem is that I can't make mmap() working. Here is the simplest code:
Code:
extern printf extern exit extern mmap %define PROT_NONE 0x00 %define PROT_READ 0x01 %define PROT_WRITE 0x02 %define PROT_EXEC 0x04 %define MAP_SHARED 0x01 %define MAP_PRIVATE 0x02 %define MAP_TYPE 0x0F %define MAP_FIXED 0x10 %define MAP_ANON 0x20 section .data PrintInt db "%lld", 0xA, 0 section .text global main main: sub rsp,8 ; stack alignement mov rdi,0 mov rsi,4096 mov rdx,PROT_READ | PROT_WRITE mov r10,MAP_ANON | MAP_PRIVATE mov r8,-1 mov r9,0 ; rdi, rsi, rdx, r10, r8, r9 call mmap ; void* mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset); mov rdi,PrintInt mov rsi,rax call printf xor rdi,rdi call exit
The answer of mmap() in rax is always -1, which means MAP_FAILED.
I also tried a syscall:
Code:
mov rax,477 ; AUE_MMAP STD { caddr_t mmap(caddr_t addr, size_t len, \ int prot, int flags, int fd, off_t pos); } mov rdi,0 ; rdi, rsi, rdx, r10, r8, r9 mov rsi,4096 mov rdx,PROT_READ | PROT_WRITE mov r10,MAP_ANON | MAP_PRIVATE mov r8,-1 mov r9,0 syscall
Here, the return code is 9: Bad file descriptor.
I think it's something obvious but I'm stuck. Can someone help me?