c++ - 弱属性的函数不能被覆盖

标签 c++ c gcc arm weak-linking

我想用 weak 属性覆盖函数(中断处理程序),但链接器没有链接我的定义。代码已缩短以便更好地阅读。

vector .c

void NMI_Handler (void) __attribute__((weak));
void HardFault_Handler (void) __attribute__((weak));

__attribute__ ((section(".vectors"), used))
void (* const gVectors[])(void) = 
{
      NMI_Handler,
      HardFault_Handler
};

void NMI_Handler (void) { while(1); }
void HardFault_Handler (void) { while(1); }

我在cpuexcept.cpp文件中重新定义了默认定义

extern "C" __attribute__((naked))
void NMI_Handler()
{  
    EXCEPT_ENTRY(CPUExcept);
}

extern "C" __attribute__((naked))
void HardFault_Handler()
{  
    EXCEPT_ENTRY(CPUExcept);
}

如果我编译并转储它,输出(库 lib.a)是:

cpuexcept.oo:     file format elf32-littlearm
rw-rw-rw- 0/0   4728 Jun 26 16:20 2012 cpuexcept.oo
architecture: arm, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000
private flags = 5000000: [Version5 EABI]

Sections:
Idx Name          Size      VMA       LMA       File off  Algn  Flags
  0 .text         0000051c  00000000  00000000  00000034  2**2  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000000  00000000  00000000  00000550  2**0  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  00000000  00000000  00000550  2**0  ALLOC
  3 .rodata       000001dc  00000000  00000000  00000550  2**2  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .comment      00000012  00000000  00000000  0000072c  2**0  CONTENTS, READONLY
  5 .ARM.attributes 00000031  00000000  00000000  0000073e  2**0  CONTENTS, READONLY
SYMBOL TABLE:
00000000 l    df *ABS*  00000000 cpuexcept.cpp
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
000004e0 g     F .text  0000000a NMI_Handler
000004ec g     F .text  0000000a HardFault_Handler

000004e0 <NMI_Handler>:
4e0:    e92d 0ff0   stmdb   sp!, {r4, r5, r6, r7, r8, r9, sl, fp}
4e4:    4668        mov r0, sp
4e6:    f7ff fffe   bl  c0 <CPUExcept>  4e6: R_ARM_THM_CALL CPUExcept
4ea:    bf00        nop

000004ec <HardFault_Handler>:
4ec:    e92d 0ff0   stmdb   sp!, {r4, r5, r6, r7, r8, r9, sl, fp}
4f0:    4668        mov r0, sp
4f2:    f7ff fffe   bl  c0 <CPUExcept>  4f2: R_ARM_THM_CALL CPUExcept
4f6:    bf00        nop

vectors.o:     file format elf32-littlearm
rw-rw-rw- 0/0   4464 Jun 27 13:52 2012 vectors.o
architecture: arm, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000
private flags = 5000000: [Version5 EABI]

Sections:
Idx Name          Size      VMA       LMA       File off  Algn  Flags
  0 .text         00000114  00000000  00000000  00000034  2**2  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000000  00000000  00000000  00000148  2**0  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  00000000  00000000  00000148  2**0  ALLOC
  3 .vectors      00000130  00000000  00000000  00000148  2**2  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
  4 .comment      00000012  00000000  00000000  00000278  2**0  CONTENTS, READONLY
  5 .ARM.attributes 00000031  00000000  00000000  0000028a  2**0  CONTENTS, READONLY
SYMBOL TABLE:
00000000 l    df *ABS*  00000000 vectors.c
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
00000000 l    d  .vectors   00000000 .vectors
00000000 l    d  .comment   00000000 .comment
00000000 l    d  .ARM.attributes    00000000 .ARM.attributes
00000000  w    F .text  00000002 NMI_Handler
00000004  w    F .text  00000002 HardFault_Handler

00000000 <NMI_Handler>:
0:  e7fe        b.n 0 <NMI_Handler>
2:  bf00        nop

00000004 <HardFault_Handler>:
4:  e7fe        b.n 4 <HardFault_Handler>
6:  bf00        nop

具有弱属性的默认函数链接到目标应用程序。如果我在 cpuexcept.cpp 中定义函数 f() 并在主函数中使用它,或者如果我的处理程序定义在其他 .c 模块中,我的定义链接正确。 我在 cygwin 中使用 arm-none-eabi-gcc 4.6.2 (YAGARTO) 编译器。

最佳答案

你可以尝试像这样定义你的函数
void NMI_Handler(void)
而不是
无效 NMI_Handler()

作为弱定义也是void NMI_Handler (void) __attribute__((weak));
因此链接器不会发现任何差异。

关于c++ - 弱属性的函数不能被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11226389/

相关文章:

c++ - 服务阻止 Windows 启动

c - 将第一个数组索引放在数组末尾

c - C中的双指针const正确性警告

c++ - 忽略 C++ 中断言表达式的副作用

gcc - iostream.h,找不到fstream.h

c++ - 下一个排列定义

c++ - 仅读取共享内存时的互斥锁定

c++ - 函数模板不识别左值

c - 测量功能前后的挂钟时间

c - 将静态库与 C 数学库正确链接