8

メモリをヒープに動的に割り当て、それらのメモリ アドレスに値を割り当てようとしています。メモリを割り当てる方法は理解していますが、たとえばレジスタの値を最初の動的メモリ アドレスに割り当てるにはどうすればよいでしょうか? これは私がこれまでに持っているものです:

    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

movたとえば、値をにraxするにはどうすればよいでしょうかbrk_firstLocation

4

4 に答える 4

9

他の人は、あなたのコードに問題があることをいくつか指摘しています。現在のブレークポイントに20ビット(または実際には20バイト) を追加するのではなく、単に 5 バイトを追加することを追加したいと思います。add rbx, 20

また、最初の syscall 引数は rbx ではなく、rdi になります。 64 ビット syscall ABIは、32 ビット ABI (64 ビット プロセスで引き続き使用可能)とは異なるシステム コール番号、異なるレジスタ、および (syscallの代わりに) 異なる命令を使用します。タグ wikiint 0x80も参照してください。

コードは次のようになります。

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.
于 2017-07-03T02:11:36.740 に答える