[英]malloc and free x86 NASM + compiling
I have been teaching myself x86 assembly and have been looking at doing basic malloc() and free() calls. 我一直在教自己x86程序集,并一直在寻找基本的malloc()和free()调用。 I have spent quite a bit of time searching but most examples are for 64-bit or only show the malloc call without the free, etc. I even wrote this in c, then compiled and disassembled it, which helped but gcc adds a lot of other instructions.
我花了相当多的时间搜索,但大多数的例子是64位或只显示没有免费的malloc调用等。我甚至在c中写了这个,然后编译和反汇编它,这有帮助但是gcc增加了很多其他说明。
Here is a basic example I made of what I was able to figure out, please let me know if this is correct or if there is anything else I should be doing: 以下是我能够弄清楚的基本示例,请告诉我这是否正确或者我还有什么要做的:
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
The second part to my question is compiling it. 我的问题的第二部分是编译它。 From what I was able to find I need to link
/lib/ld-linux.so.2
. 根据我的能力,我需要链接
/lib/ld-linux.so.2
。 So in my makefile I have the following but it errors out: 所以在我的makefile中我有以下但是它出错了:
mem: mem.asm
nasm -f elf mem.asm
ld -melf_i386 -lc -I /lib/ld-linux.so.2 mem.o -o mem
This is the error I get when trying to compile: 这是我在尝试编译时遇到的错误:
As I said I am a noob at x86 so if you also have any comments for better ways to do things I would appreciate those too! 正如我所说,我是x86的菜鸟,所以如果你对更好的做事方式有任何意见,我也会很感激! :)
:)
UPDATE : 更新 :
So I went ahead and used gcc and got that to work (without and errors at least): 所以我继续使用gcc并使其工作(至少没有和错误):
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
However when I went to run it it crashed big time: 然而,当我去运行它时,它崩溃了很长时间:
I am clearly doing something wrong with free
but as I mentioned, I wasn't positive about my use of malloc
and free
since I couldn't find any solid examples. 我显然做了一些
free
错误,但正如我所提到的,我对使用malloc
和free
是不正面的,因为我找不到任何可靠的例子。 Any clues? 有线索吗?
Thanks to everyone for the help! 感谢大家的帮助! So first I was able to get the linking errors fixed by using
gcc
to link instead of ld
: 所以首先我能够通过使用
gcc
链接而不是ld
来修复链接错误:
mem: mem.asm
nasm -f elf mem.asm
gcc -m32 mem.o -o mem
In order to get that to work I needed to change the names of the functions from _malloc
and _free
to malloc
and free
. 为了实现这一点,我需要将函数的名称从
_malloc
和_free
为malloc
和free
。 I also had to change the standard global _start
to global main
in order to get gcc happy. 我还必须将标准的
global _start
更改为global main
,以便让gcc满意。 This let it compile and link without errors but as you saw in the update the program crashed horribly when it came time to free the memory. 这让它编译和链接没有错误,但正如你在更新中看到的那样,程序在释放内存时崩溃了。
This was because I was pushing the wrong address to the stack. 这是因为我把错误的地址推到了堆栈。 I initially had the instruction
push mptr
but that was pushing the address of mptr
to the stack rather than the address it was pointing to, hence the error. 我最初有指令
push mptr
但这是将mptr
的地址推送到堆栈而不是它指向的地址,因此错误。 A simple update to the instruction in order to push the correct address to the stack allowed my simple program to run without errors: 对指令进行简单更新以便将正确的地址压缩到堆栈允许我的简单程序运行而不会出现错误:
push dword [mptr]
The final result: 最终结果:
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
Thanks again to everyone for the help and thanks Peter Cordes for giving me a chance to provide a proper answer! 再次感谢大家的帮助,感谢Peter Cordes让我有机会提供正确的答案! :)
:)
I'm sure i'll be back with more noob x86 questions as my journey continues! 随着我的旅程继续,我相信我会回来更多noob x86问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.