简体   繁体   English

为什么gcc不支持裸功能?

[英]Why doesn't gcc support naked functions?

I use naked functions to patch parts of a program while it's running. 我在程序运行时使用裸函数来修补程序的各个部分。 I can easily do this in VC++ in Windows. 我可以在Windows中的VC ++中轻松完成此操作。 I'm trying to do this in Linux and it seems gcc doesn't support naked functions. 我试图在Linux中这样做,似乎gcc不支持裸功能。 Compiling code with naked functions gives me this: warning: 'naked' attribute directive ignored. 用裸函数编译代码给了我:警告:'naked'属性指令被忽略了。 Compiled under CentOS 5.5 i386. 在CentOS 5.5 i386下编译。

The naked attribute is only supported by GCC on certain platforms (ARM, AVR, MCORE, RX and SPU) according to the docs : 根据文档 ,裸属性仅在某些平台(ARM,AVR,MCORE,RX和SPU)上由GCC支持:

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. naked :在ARM,AVR,MCORE,RX和SPU端口上使用此属性指示指定的函数不需要编译器生成的序言/结尾序列。 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. 可以安全地包含在裸函数中的唯一语句是没有操作数的asm语句。 All other statements, including declarations of local variables, if statements, and so forth, should be avoided. 应避免使用所有其他语句,包括声明局部变量,if语句等。 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. 我不知道为什么。

On x86 you can workaround by using asm at global scope instead: 在x86上,您可以在全局范围内使用asm来解决此问题:

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. x86函数属性中也列出了naked ,所以我认为它适用于较新的gcc。

GCC only supports naked functions on ARM and other embedded platforms. GCC仅支持ARM和其他嵌入式平台上的裸功能。 http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 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. 此外,您正在做的事情本质上是不安全的,因为如果程序正在运行,您无法保证您正在修补的代码没有执行。

That's an ugly solution. 这是一个丑陋的解决方案。 Link against a .asm file for your target architecture. 链接目标体系结构的.asm文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM