我尝试将内存动态分配到堆中,然后在这些内存地址中赋值。我知道如何分配内存,但是我如何将寄存器中的值分配给第一个动态内存地址呢?这就是我到目前为止所知道的:
push rbp
mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines
mov rax, 0x2d ;linux system call for brk()
mov rbx, 0x0 ;to get the adress of the first adress we are allocating we must have 0 in rbx
int 0x80 ;calls the linux operating system kernel for assistance
mov [brk_firstLocation], rax ;the first position in the heap will be returned in rax thus i save the first loaction in a varable called brk_firstLocation
mov rbx, rax ;the memory adress of the start of the heap is moved in rbx
add rbx, 0x14 ;we want 5 bytes worth of data alocated in the heap, so the start adress plus 20 bits
mov rax, 0x2d ;linux system call for brk()
int 0x80 ;calls the linux operating system kernel for assistance例如,我该怎么做才能将rax中的值mov成brk_firstLocation
发布于 2017-07-03 10:11:37
其他人已经指出了您的代码中的一些错误。我想补充的是,您不会向当前断点添加20位(或像add rbx, 20实际所做的20个字节),您只需添加5个字节。
此外,您的第一个syscall参数不会在rbx中,它将在rdi中。与32位The 64-bit syscall ABI (在64位进程中仍然可用)相比,ABI使用不同的系统调用号、不同的寄存器和不同的指令(syscall而不是int 0x80)。有关更多x86链接,请参阅ABI。
下面是你的代码应该是什么样子:
push rbp
mov rbp, rsp
;; sys_brk(0)
mov rax, 12 ; 12 is SYS_brk (/usr/include/asm/unistd_64.h)
mov rdi, 0 ; rdi for first syscall arg in the 64-bit ABI, not rbx
syscall ; syscall, not int 0x80, for the 64-bit ABI
mov qword [brk_firstLocation], rax
;; sys_brk(old_break + 5)
lea rdi, [rax + 5] ; add 5 bytes to the break point
mov rax, 12
syscall ; set the new breakpoint此时,您可以使用brk_firstLocation作为指针,指向您想要存储在堆上的任何5字节结构。下面是如何将值放入内存空间:
mov rdi, [brk_firstLocation] ; load the pointer from memory, if you didn't already have it in a register
mov byte [rdi], 'A' ; a char in the first byte
mov [rdi+1], ecx ; a 32-bit value in the last 4 bytes.发布于 2014-03-23 14:32:29
int 80h仅适用于32位系统调用。请改用64位的syscall。
调用sys_brk两次是多余的-在汇编中,你总是知道你的程序数据在哪里结束。只需在那里放一个标签,你就会有地址了。
以这种方式分配少于一页的内存是没有意义的,它将以4KB的块进行分配。
理解这一点很重要-- sys_brk不是堆管理函数。它是低级内存管理。
发布于 2014-03-23 12:37:37
我看到了一些问题:
所以你想要这样的东西:
mov [brk_firstloaction], _end
mov rbx, [brk_firstlocation]
add rbx, 0x14 ; space for 5 dwords (20 bytes)
mov rax, 12
int 0x80https://stackoverflow.com/questions/22586532
复制相似问题