Reputation: 235
I have problem with writing MMAP2 in ASM AT&T and call it in C. I wrote this but didn't know how should it works. I am knowingly that code is not good but I very need help.
Can you tell me how should it looks ?
Thanks for help!
.data
MMAP2 = 192
MUNMAP = 91
PROT_READ = 0x1
MAP_ANONYMOUS = 0x20
.bss
.text
.global moje_mmap
.type moje_map @function
moje_mmap:
push %ebp
mov %esp, %ebp
xor %ebx, %ebx
mov 8(%ebp), %ecx
mov $PROT_READ, %edx
mov $MAP_ANONYMOUS, %esi
mov $-1, %edi
mov $MMAP2, %eax
int $0x80
mov %ebp, %esp
pop %ebp
ret
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
void* moje_mmap(size_t dlugosc);
int main() {
moje_mmap(30);
return 0; }
Upvotes: 1
Views: 893
Reputation: 1887
You're actually returning the value correctly from your assembly function. -22 is a valid return value from mmap2, meaning EINVAL. When you use syscalls directly from assembly, errors are usually returned as the negative version of the error, for example -EINVAL or -22.
Now, as to why you're getting an error, here's an excerpt from the mmap2 man page:
EINVAL (Various platforms where the page size is not 4096 bytes.)
offset * 4096 is not a multiple of the system page size.
Looking at your code, you're passing -1 as the offset parameter, but that's valid, so it's not the problem.
The problem is more likely to be with your flags
parameter:
The flags argument determines whether updates to the mapping are
visible to other processes mapping the same region, and whether
updates are carried through to the underlying file. This behavior is
determined by including exactly one of the following values in flags:
MAP_SHARED Share this mapping. Updates to the mapping are visible to
other processes that map this file, and are carried
through to the underlying file. (To precisely control
when updates are carried through to the underlying file
requires the use of msync(2).)
MAP_PRIVATE
Create a private copy-on-write mapping. Updates to the
mapping are not visible to other processes mapping the
same file, and are not carried through to the underlying
file. It is unspecified whether changes made to the file
after the mmap() call are visible in the mapped region.
As stated here, you must include either MAP_SHARED or MAP_PRIVATE in your flags parameter. Add this, and your program should work.
Upvotes: 1