Why doesn't gcc support naked functions?

北慕城南 提交于 2019-12-23 09:39:26

问题


I use naked functions to patch parts of a program while it's running. I can easily do this in VC++ in Windows. I'm trying to do this in Linux and it seems gcc doesn't support naked functions. Compiling code with naked functions gives me this: warning: ‘naked’ attribute directive ignored. Compiled under CentOS 5.5 i386.


回答1:


The naked attribute is only supported by GCC on certain platforms (ARM, AVR, MCORE, RX and SPU) according to the docs:

naked: Use this attribute on the ARM, AVR, MCORE, RX and SPU ports to indicate that the specified function does not need prologue/epilogue sequences generated by the compiler. It is up to the programmer to provide these sequences. The only statements that can be safely included in naked functions are asm statements that do not have operands. All other statements, including declarations of local variables, if statements, and so forth, should be avoided. Naked functions should be used to implement the body of an assembly function, while allowing the compiler to construct the requisite function declaration for the assembler.

I'm not sure why.




回答2:


On x86 you can workaround by using asm at global scope instead:

int write(int fd, const void *buf, int count);                                            

asm                                                                              
(                                                                                
".global write                             \n\t"                                    
"write:                                    \n\t"
"       pusha                              \n\t"                                    
"       movl   $4, %eax                    \n\t"                                    
"       movl   36(%esp), %ebx              \n\t"                                    
"       movl   40(%esp), %ecx              \n\t"                                    
"       movl   44(%esp), %edx              \n\t"                                    
"       int    $0x80                       \n\t"                                    
"       popa                               \n\t"                                    
"       ret                                \n\t"                                    
);                                                                               

void _start()                                                                    
{                                                                                
#define w(x) write(1, x, sizeof(x));                                             
    w("hello\n");                                                                
    w("bye\n");                                                                  
}                                                                                

Also naked is listed among x86 function attributes, so I suppose it works for newer gcc.




回答3:


GCC only supports naked functions on ARM and other embedded platforms. http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

Also, what you're doing is inherently unsafe, as you cannot guarantee that the code you're patching isn't executing if the program is running.




回答4:


That's an ugly solution. Link against a .asm file for your target architecture.



来源:https://stackoverflow.com/questions/7278234/why-doesnt-gcc-support-naked-functions

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