编写汇编函数时,序言和结语是强制性的吗? [英] Are the prologue and epilogue mandatory when writing assembly functions?

查看:195
本文介绍了编写汇编函数时,序言和结语是强制性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在汇编中重写了一些libc函数,对于其中的一些(不需要任何 call syscall 指令,例如strlen),我舍弃了序言和结尾,因为没有它我的测试并没有失败(也许它们不够复杂).在同行评审中,有人告诉我丢弃它们是不好的做法,但无法解释原因.

Recently I rewrote some libc functions in assembly and for some of them (the one that didn't need any call or syscall instructions, like strlen), I discarded the prologue and epilogue because my tests didn't failed without it (maybe they were not complex enough). During peer review, someone told me it was bad practice to discard them, but couldn't explain me why.

那么,当我调用没有序言/结语组合的asm函数时,是否会遇到问题?

So, am I running into problems when I call asm functions that don't have the prologue/epilogue combination?

即使在堆栈上不需要额外空间的情况下,添加它也是一种好习惯吗?

Is it a good practice to add it even when no additionnal space is needed on the stack?

如果出于某些原因是强制性的,为什么汇编程序(我使用nasm)不负责?

If mandatory for some reasons, why doesn't the assembler (I used nasm) take care of it?

推荐答案

如果您没有设置正确的堆栈框架,调试器可能很难知道您现在所处的功能.在ELF目标上,您必须手动提供CFI数据(参见本文),如果您未明确设置堆栈框架.没有CFI数据,堆栈展开将无法进行,并且调试器可能无法找出您要使用的功能.除非您要手动添加CFI数据(这有些乏味且容易出错),否则建议您执行以下操作:接受轻微的性能损失,然后设置一个完整的堆栈框架.

If you do not set up a proper stack frame, it can be hard for a debugger to know what function you are in right now. On ELF-targets, you have to manually provide CFI data (cf. this article) if you do not explicitly set up a stack frame. Without CFI data, stack unwinding doesn't work and the debugger might not be able to find out what function you are in. Unless you want to manually add CFI data (which is somewhat tedious and easy to get wrong), I recommend you to accept the minor performance loss and just set up a full stack frame.

这篇关于编写汇编函数时,序言和结语是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆