malloc 和 free x86 NASM + 编译
malloc and free x86 NASM + compiling
我一直在自学 x86 汇编,并一直在研究基本的 malloc() 和 free() 调用。我花了很多时间搜索,但大多数示例都是针对 64 位的,或者只显示没有免费的 malloc 调用等。我什至用 c 写了这个,然后编译和反汇编它,这有帮助,但 gcc 增加了很多其他说明。
这是我根据自己的想法制作的一个基本示例,请告诉我这是否正确,或者我是否应该做其他事情:
global _start
; glibc stuff
extern _malloc, _free
section .data
err: db "malloc failed!", 10, 0
.len: equ $ - err
section .bss
mptr resd 1 ;pointer to begining of malloc'd memory
section .text
_start:
push 20 ;allocate 20 bytes
call _malloc ;call malloc
add esp, 4 ;clean pushed imm
test eax, eax ;check for malloc error
jz merror
mov [mptr], eax ;store address
mov byte [eax], 0
mov byte [eax + 1], 1
push mptr ;push address
call _free ;call free
add esp, 4 ;clean push
exit:
mov eax, 0x1
int 80h
merror:
mov eax, 0x4
mov ebx, 0x1
mov ecx, err
mov edx, err.len
int 80h
jmp exit
我的问题的第二部分是编译它。根据我能够找到的内容,我需要 link /lib/ld-linux.so.2
。所以在我的 makefile 中我有以下但它出错了:
mem: mem.asm
nasm -f elf mem.asm
ld -melf_i386 -lc -I /lib/ld-linux.so.2 mem.o -o mem
这是我在尝试编译时遇到的错误:
正如我所说,我是 x86 的菜鸟,所以如果您对更好的做事方式有任何意见,我也将不胜感激! :)
更新 :
所以我继续使用 gcc 并让它工作(至少没有错误):
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
然而,当我去 运行 时,它崩溃了很长时间:
我显然在 free
上做错了,但正如我提到的,我对 malloc
和 free
的使用并不乐观,因为我找不到任何可靠的东西例子。有什么线索吗?
感谢大家的帮助!所以首先我能够通过使用 gcc
到 link 而不是 ld
:
来修复 linking 错误
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
为了让它工作,我需要将函数的名称从 _malloc
和 _free
更改为 malloc
和 free
。为了让 gcc 满意,我还必须将标准 global _start
更改为 global main
。这让它可以编译并且 link 没有错误,但是正如您在更新中看到的那样,当需要释放内存时,程序会严重崩溃。
这是因为我将错误的地址压入堆栈。我最初有指令 push mptr
但那是将 mptr
的地址推入堆栈而不是它指向的地址,因此出现错误。为了将正确的地址推送到堆栈,对指令进行了简单的更新,使我的简单程序可以 运行 没有错误:
push dword [mptr]
最终结果:
global main
; glibc stuff
extern malloc, free
section .data
err: db "malloc failed!", 10, 0
.len: equ $ - err
section .bss
mptr resd 1 ;pointer to begining of malloc'd memory
section .text
main:
push 20 ;allocate 20 bytes
call malloc ;call malloc
add esp, 4 ;clean pushed imm
test eax, eax ;check for malloc error
jz merror
mov [mptr], eax ;store address
mov byte [eax], 0 ;store 0 at index 0
mov byte [eax + 1], 1 ;store 1 at index 1
push dword [mptr] ;push address
call free ;call free
add esp, 4 ;clean push
exit:
mov eax, 0x1
int 80h
merror:
mov eax, 0x4
mov ebx, 0x1
mov ecx, err
mov edx, err.len
int 80h
jmp exit
再次感谢大家的帮助,感谢Peter Cordes让我有机会提供正确的答案! :)
我相信随着我的旅程继续,我会带着更多新手 x86 问题回来!
我一直在自学 x86 汇编,并一直在研究基本的 malloc() 和 free() 调用。我花了很多时间搜索,但大多数示例都是针对 64 位的,或者只显示没有免费的 malloc 调用等。我什至用 c 写了这个,然后编译和反汇编它,这有帮助,但 gcc 增加了很多其他说明。
这是我根据自己的想法制作的一个基本示例,请告诉我这是否正确,或者我是否应该做其他事情:
global _start
; glibc stuff
extern _malloc, _free
section .data
err: db "malloc failed!", 10, 0
.len: equ $ - err
section .bss
mptr resd 1 ;pointer to begining of malloc'd memory
section .text
_start:
push 20 ;allocate 20 bytes
call _malloc ;call malloc
add esp, 4 ;clean pushed imm
test eax, eax ;check for malloc error
jz merror
mov [mptr], eax ;store address
mov byte [eax], 0
mov byte [eax + 1], 1
push mptr ;push address
call _free ;call free
add esp, 4 ;clean push
exit:
mov eax, 0x1
int 80h
merror:
mov eax, 0x4
mov ebx, 0x1
mov ecx, err
mov edx, err.len
int 80h
jmp exit
我的问题的第二部分是编译它。根据我能够找到的内容,我需要 link /lib/ld-linux.so.2
。所以在我的 makefile 中我有以下但它出错了:
mem: mem.asm
nasm -f elf mem.asm
ld -melf_i386 -lc -I /lib/ld-linux.so.2 mem.o -o mem
这是我在尝试编译时遇到的错误:
正如我所说,我是 x86 的菜鸟,所以如果您对更好的做事方式有任何意见,我也将不胜感激! :)
更新 :
所以我继续使用 gcc 并让它工作(至少没有错误):
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
然而,当我去 运行 时,它崩溃了很长时间:
我显然在 free
上做错了,但正如我提到的,我对 malloc
和 free
的使用并不乐观,因为我找不到任何可靠的东西例子。有什么线索吗?
感谢大家的帮助!所以首先我能够通过使用 gcc
到 link 而不是 ld
:
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
为了让它工作,我需要将函数的名称从 _malloc
和 _free
更改为 malloc
和 free
。为了让 gcc 满意,我还必须将标准 global _start
更改为 global main
。这让它可以编译并且 link 没有错误,但是正如您在更新中看到的那样,当需要释放内存时,程序会严重崩溃。
这是因为我将错误的地址压入堆栈。我最初有指令 push mptr
但那是将 mptr
的地址推入堆栈而不是它指向的地址,因此出现错误。为了将正确的地址推送到堆栈,对指令进行了简单的更新,使我的简单程序可以 运行 没有错误:
push dword [mptr]
最终结果:
global main
; glibc stuff
extern malloc, free
section .data
err: db "malloc failed!", 10, 0
.len: equ $ - err
section .bss
mptr resd 1 ;pointer to begining of malloc'd memory
section .text
main:
push 20 ;allocate 20 bytes
call malloc ;call malloc
add esp, 4 ;clean pushed imm
test eax, eax ;check for malloc error
jz merror
mov [mptr], eax ;store address
mov byte [eax], 0 ;store 0 at index 0
mov byte [eax + 1], 1 ;store 1 at index 1
push dword [mptr] ;push address
call free ;call free
add esp, 4 ;clean push
exit:
mov eax, 0x1
int 80h
merror:
mov eax, 0x4
mov ebx, 0x1
mov ecx, err
mov edx, err.len
int 80h
jmp exit
再次感谢大家的帮助,感谢Peter Cordes让我有机会提供正确的答案! :)
我相信随着我的旅程继续,我会带着更多新手 x86 问题回来!