Gcc裸属性留下一些尾随函数序言asm指令

use*_*939 2 gcc arm system-calls armv6 cortex-m

我在cortex-m0上有一个svc异常处理程序的以下实现:

int  __attribute__((naked))  
sv_call_handler(uint32_t n, uint32_t arg1, uint32_t arg2, uint32_t arg3,  
                uint32_t arg4, uint32_t arg5)
 {
      irq_off();
Run Code Online (Sandbox Code Playgroud)

当我为cortex-m0构建它时,它看起来像这样:

   0x7a50 <sv_call_handler>        movs   r4, r0                                                                    
   0x7a52 <sv_call_handler+2>      str    r1, [r7, #12]                                                             
   0x7a54 <sv_call_handler+4>      str    r2, [r7, #8]                                                              
   0x7a56 <sv_call_handler+6>      str    r3, [r7, #4]                                                              
   0x7a58 <sv_call_handler+8>      bl     0x3194 <irq_off> 
Run Code Online (Sandbox Code Playgroud)

当然,导致硬故障,R7中的值是"未定义的",并且它很可能包含不在地址范围内的值.

当我删除裸属性时,程序集更有意义:

   0x7a50 <sv_call_handler>        push   {r4, r5, r7, lr}                                                          
   0x7a52 <sv_call_handler+2>      sub    sp, #32                                                                   
   0x7a54 <sv_call_handler+4>      add    r7, sp, #8                                                                
   0x7a56 <sv_call_handler+6>      str    r0, [r7, #12]
Run Code Online (Sandbox Code Playgroud)

我之前没有使用过裸属性,为什么现在会发生这种情况呢?与svc异常处理程序是一个特殊情况这一事实有什么关系吗?

Joh*_*han 6

根据gcc手册,您只能将naked函数属性用于仅包含基本asm语句的函数.asm在这样的函数中使用扩展语句或普通的c代码似乎可以工作,但不能保证这样做.

在您的代码中,您可以在裸功能中进行正常的函数调用.这可能是你问题的根源.