Equivalent for GCC's naked attribute

我的未来我决定 提交于 2019-12-18 02:47:06

问题


I've got an application written in pure C, mixed with some functions that contain pure ASM. Naked attribute isn't available for x86 (why? why?!) and my asm functions don't like when prologue and epilogue is messing with the stack. Is it somehow possible to create a pure assembler function that can be referenced from C code parts? I simply need the address of such ASM function.


回答1:


Just use asm() outside a function block. The argument of asm() is simply ignored by the compiler and passed directly on to the assembler. For complex functions a separate assembly source file is the better option to avoid the awkward syntax.

Example:

#include <stdio.h>

asm("_one:              \n\
        movl $1,%eax    \n\
        ret             \n\
");

int one();

int main() {
        printf("result: %d\n", one());
        return 0;
}

PS: Make sure you understand the calling conventions of your platform. Many times you can not just copy/past assembly code.

PPS: If you care about performance, use extended asm instead. Extended asm essentially inlines the assembly code into your C/C++ code and is much faster, especially for short assembly functions. For larger assembly functions a seperate assembly source file is preferable, so this answer is really a hack for the rare case that you need a function pointer to a small assembly function.




回答2:


Good news everyone. GCC developers finally implemented attribute((naked)) for x86. The feature will be available in GCC 8.




回答3:


Certainly, just create a .s file (assembly source), which is run through gas (the assembler) to create a normal object file.



来源:https://stackoverflow.com/questions/8374449/equivalent-for-gccs-naked-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!