首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序集x86 brk()调用用法

程序集x86 brk()调用用法
EN

Stack Overflow用户
提问于 2014-03-23 10:59:22
回答 4查看 5.1K关注 0票数 8

我尝试将内存动态分配到堆中,然后在这些内存地址中赋值。我知道如何分配内存,但是我如何将寄存器中的值分配给第一个动态内存地址呢?这就是我到目前为止所知道的:

代码语言:javascript
复制
    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中的值movbrk_firstLocation

EN

回答 4

Stack Overflow用户

发布于 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。

下面是你的代码应该是什么样子:

代码语言:javascript
复制
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字节结构。下面是如何将值放入内存空间:

代码语言:javascript
复制
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.
票数 9
EN

Stack Overflow用户

发布于 2014-03-23 14:32:29

int 80h仅适用于32位系统调用。请改用64位的syscall

调用sys_brk两次是多余的-在汇编中,你总是知道你的程序数据在哪里结束。只需在那里放一个标签,你就会有地址了。

以这种方式分配少于一页的内存是没有意义的,它将以4KB的块进行分配。

理解这一点很重要-- sys_brk不是堆管理函数。它是低级内存管理。

票数 2
EN

Stack Overflow用户

发布于 2014-03-23 12:37:37

我看到了一些问题:

  • 0x2d是x86 (32位)上的brk系统调用;在x86_64上,它的0xc
  • brk设置数据段的结束;成功时返回0,失败时返回-1。它不会返回“堆中的第一个位置”。这来自于链接器设置到未初始化预分配数据末尾的符号_end。

所以你想要这样的东西:

代码语言:javascript
复制
    mov [brk_firstloaction], _end
    mov rbx, [brk_firstlocation]
    add rbx, 0x14         ; space for 5 dwords (20 bytes)
    mov rax, 12
    int 0x80
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22586532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档