【问题标题】:Gcc naked attribute leaves some trailing function prologue asm instructionsGcc 裸属性留下一些尾随函数序言 asm 指令
【发布时间】:2017-10-23 06:44:42
【问题描述】:

对于 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();

当我为 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> 

导致硬故障,因为 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]

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

【问题讨论】:

    标签: gcc arm system-calls cortex-m armv6


    【解决方案1】:

    根据 gcc 手册,您只能将 naked 函数属性用于仅包含基本 asm 语句的函数。在此类函数中使用扩展的asm 语句或普通的 c 代码可能会起作用,但不能保证会发生什么。

    在您的代码中,您的裸函数中有一个正常的函数调用。这可能是您问题的根源。

    【讨论】:

      【解决方案2】:

      不要使用带参数的裸函数。这些参数必须由一些序言处理,这很可能不是您想要的。

      最好在汇编中编写 (IMO)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-12
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 2015-11-15
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多