Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't gcc support naked functions?

Tags:

c++

linux

gcc

g++

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.

like image 266
ares94 Avatar asked Sep 02 '11 00:09

ares94


2 Answers

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.

like image 180
David Grayson Avatar answered Nov 10 '22 11:11

David Grayson


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.

like image 4
Uprooted Avatar answered Nov 10 '22 09:11

Uprooted