2

I would like to overwrite function (interupt handlers) with the weak attribute, but linker does not link my definition. Codes are shorted for better reading.

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); }

I redefine default definition in the file cpuexcept.cpp

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

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

If I compile and dump it, output (library lib.a) is:

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

Default function with the weak attribute is linked in to target application. My definition is linked correct, if I define function f() in cpuexcept.cpp and I use it in main function or if my definiton of handler is in other .c module. I use arm-none-eabi-gcc 4.6.2 (YAGARTO) compiler in cygwin.

4

1 回答 1

0

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

由于弱定义也是void NMI_Handler (void) __attribute__((weak));
如此,因此链接器不会看到任何区别。

于 2012-06-27T12:59:44.037 回答