GCC arm-none-eabi (Codesourcery) and C++ Exception

2020-03-03 05:41发布

问题:

I am using Raisonance's Ride7/Codesourcery (a.k.a Sourcery CodeBench Lite) with an STM32F4 board developing a bare metal HMI platform.

I will be making use of C++ exceptions in this system, but any exception I throw ends with a "Terminate called recursively" error written to stderr.

Code to reproduce the problem: (main.cpp)

int main(void)
{
    try {
        throw 1;
    }
    catch (...) {
        printf("caught");
    }
}

I've already tried Raisonance and other sources for a resolution, and have not received any actionable help.

Potential problem/solution 1:

I've asked on other forums and they mention I need to call static constructions in my startup assembly file to initialize the unwind tables (at least that's what I think they are talking about), but I have no idea how to do this.

Potential problem/solution 2

I have also discovered a bug in binutils/gas that may be the source of my problems here (http://sourceware.org/bugzilla/show_bug.cgi?id=13449). I've been trying to build my own version of the toolchain with this patch, but that's turning into a project of its own, and have not yet succeeded.

The Question

Do I need to do something in code to make use of C++ exceptions, or is this likely a bug in the toolchain? If the former, please elaborate.

回答1:

After some persuasion that shouldn't have been necessary, Raisonance finally came through with a modification to their default linker script that fixed the problem. It may not be legal for me to post the entire linker script, but here's the knowledge that one needs to know

Add this to the .text section

*(.eh_frame)

Add these sections (name YourMemory according to the memory blocks you've set up in your linker script. Mine was Flash)

.ARM.extab :
{
    *(.ARM.extab* .gnu.linkonce.armextab.*)
} >YourMemory

.ARM :
{
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
} >YourMemory

Ensure this line occurs in the bss section

*(.bss*)

While on this quest, I ran across the following useful resources

  • http://wiki.osdev.org/C%2B%2B_Exception_Support
  • https://docs.blackfin.uclinux.org/doku.php?id=toolchain:bare_metal:link
  • http://gcc.gnu.org/ml/gcc-help/2011-07/msg00112.html
  • http://www.airs.com/blog/archives/460