2

I want to create an array that will contains for example 4 values Here's my code:

mov $32,%rsi # 4 x 8bytes
mov $9,%rax
mov $0,%rdi
mov $0x3,%rdx
mov $0x01,%r10
mov $0,%r9
syscall

Now I am having a new adress of alocated 32 bytes in rax?

When I am trying to put something into it , for example:

mov $0,%r14
mov $3,%rdx
mov %rdx,(%rax,%r14,8)

It gives me SIGSEGV error

7
  • 1
    What is the value of rax after the syscall? Is it MAP_FAILED (0xFFFFFFFFFFFFFFFF)? Also, for a system call the system call # goes in rax and the arguments go in order into the registers rdi rsi rdx r10 r8 r9. For void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); this means addr->rdi, length->rsi, prot->rdx, flags->r10, fd->r8 and offset->r9. Why are you not setting r8? Commented Apr 26, 2015 at 19:09
  • rax had 0xfffffffffffffff7 after the syscall. r8 was default as 0 , I've changed it into -1 mov $-1,%r8 and set r10 to 3 (map_anonymous) now I have received rax 0xffffffffffffffff(map_failed) Commented Apr 26, 2015 at 19:36
  • You must keep the MAP_PRIVATE flag and OR into it MAP_ANONYMOUS. The rule is that exactly one of either MAP_PRIVATE or MAP_SHARED, but not both, must be specified, and in addition zero or more other flags, including MAP_ANONYMOUS. Commented Apr 26, 2015 at 19:59
  • Now I put 1 into r10 mov $1,%r10 as MAP_PRIVATE , the rax after syscal is now 0xffffffffffffffed , but still when I want to do an operation on this mov %rdx,(%rax,%r14,8) it gives me SIGSEGV error Commented Apr 26, 2015 at 20:06
  • 1
    I have found that MAP_PRIVATE is 0x1 and MAP_ANONYMOUS is 0x3, after OR operation on 01 and 11 I got 11 which is the same as 0x3. I've tried putting 1,2,3,4,5,6,7 number in r10 always ending with SIGSEGV error. Commented Apr 26, 2015 at 20:52

1 Answer 1

2

mmap expects 6 arguments, but you only pass 5. You forgot to set up r8 for the file descriptor. Assuming you want to allocate anonymous memory, this should be set to -1 and of course the MAP_ANONYMOUS should also be set in the flags.

PS: if you have the C library available, the easy way to allocate memory is to just call malloc().

Sign up to request clarification or add additional context in comments.

4 Comments

r8 was 0, I've changed it to -1 mov $-1,%r8, and set r10 to 3 (map_anonymous). After syscall rax is 0xffffffffffffffff - map failed. I want to do it without C library
MAP_ANONYMOUS is 0x20 and MAP_PRIVATE is 0x02 (see kernel source) ... not sure where you got the wrong values from. So MAP_ANONYMOUS | MAP_PRIVATE gives 0x22.
OP did not specify the system he's on, and the values given for both flags were alien to me, but now is the time to ask... For that matter, here are the values for Mach; #define MAP_PRIVATE 0x0002 and #define MAP_ANON 0x1000. @Piodo, on what system are you developing? Linux? Mac? Something entirely different?
I am working on Ubuntu Linux. I've put 0x22 into r10 and it seems that it works. Thank you both Jester and Iwillnotexist Idonotexist for wasting your time for noobie like me . I really appreciate your help. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.