gpt4 book ai didi

linux - 程序集 x86 brk() 调用使用

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:49 27 4
gpt4 key购买 nike

我正在尝试将内存动态分配到堆中,然后在这些内存地址中分配值。我了解如何分配内存,但我如何将寄存器中的值分配给第一个动态内存地址?这是我目前所拥有的:

    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

最佳答案

其他人指出了您的代码中的一些错误。我想补充一点,您不会将 20 bits 添加到当前断点(或像 add rbx, 20 实际上那样添加 20 bytes),您只需添加 5 个字节。

此外,您的第一个系统调用参数不会在 rbx 中,而是在 rdi 中。 The 64-bit syscall ABI使用与 32 位 ABI(在 64 位进程中仍然可用)不同的系统调用号、不同的寄存器和不同的指令(syscall 而不是 int 0x80) ).另见 标记 wiki 以获取更多 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.

关于linux - 程序集 x86 brk() 调用使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com