具有弱属性的函数不能被覆盖

时间:2012-06-27 12:37:02

标签: c++ c gcc arm weak-linking

我想用weak属性覆盖函数(interupt handlers),但是链接器没有链接我的定义。代码短缺以便更好地阅读。

vectors.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

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

1 个答案:

答案 0 :(得分:0)

您可以尝试将功能定义为
void NMI_Handler(void)
而不是作为 void NMI_Handler()

由于弱定义也是void NMI_Handler (void) __attribute__((weak));
所以链接器不会看到任何差异。