Search code examples
c++cgccarmisr

Overriding Weak ISR Handler from Assembly to C++ doesn't compile any code


I am writing code for embedded programming on an ARM 32-bit based SAM D51 microprocessor using the IDE SEGGER Studio. I'm new to embedded programming and am writing my first interrupt.

The vector table with a dummy handler is written in ARM assembly, here's one example, the following code is auto-generated on project creation but marked as weak so it can be overridden in C/C++

  ldr r0, =__stack_end__
  mov sp, r0
  bl SystemInit
  b _start

  .thumb_func
  .weak SystemInit
SystemInit:
  bx lr

Anywhere I read online says to simply add a C/C++ function with identical name and it's magically used by the linker because it's not marked as weak. Below is where I'm overriding it.

void SystemInit()
{
  printf("Here");
}

However the debugger states that it can't place a breakpoint there because there's no code and in the disassembler it reveals that the entire function has been made into a comment with no code.

I've tried other functions including many of the handler functions but they all do the exact same thing and I have no idea why.

I've even tried forward declaring a weak function and then overriding it or marking the function as volatile. Here is my latest attempt but with the same result:

extern void __attribute__((weak)) SystemInit();

void SystemInit()
{
  printf("Here");
}

and another attempt

volatile void SystemInit()
{
  printf("Here");
}

They all end in no code being generated for the function and it appearing as a comment in disassembly.


Solution

  • Identifiers in C++ source files have their names mangled. The resulting linker function name is different from SystemInit. To stop C++ compiler from mangling the function name, declare the function with extern "C". That way generated function name will match the function name expected by the linker.