I am looking for a way to unwind on panics on Cortex-M targets, specifically thumbv7em-none-eabi. There are several panic handlers available [1], but no one resembles panic-unwind.
I understand that unwinding needs runtime support. I probably should write my own runtime to unwind. Essential sections, i.e. .eh_frame and .gcc_except_table, must be present in the final output ELF file for the unwinder to proceed. gimli [2] is a useful crate to parse these sections.
However, the above two sections critical to unwinding are not present in my output ELF file when my target is set to thumbv7em-none-eabi. Did I miss any compile option? Or is unwinding simply unsupported on Cortex-M?
(based on the original target spec for thumbv7em-none-eabi as you can show using rustc +nightly --target thumbv7em-none-eabi -Zunstable-options --print target-spec-json)
You can put this in a .json file and then pass the path to this file using --target instead of thumbv7em-none-eabi.
Next you will need to define the eh_personality lang item. If you are using an existing unwinder, this personality function will be called for every frame and reads .gcc_except_table to determine if unwinding should continue, cleanup should be performed or the panic is catched. If you are using a custom unwinder that doesn't call it but instead directly reads .gcc_except_table you can just make it loop.
This requires nightly and the #![feature(lang_items)] feature gate.
And then finally you need to call your unwinder from #[panic_handler]. The unwinder needs to read .eh_frame to determine how to unwind. Normally the unwinder then calls the personality function for each frame with information like the LSDA which is for GCC and LLVM a pointer into the .gcc_except_table section.