How to add exception support to newlib-nano?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to add exception support to newlib-nano?

Jump to solution
2,834 Views
v_canoz
Contributor III

Hi,

I'm using MCUXpresso with a C++ project.


Today I ran into an issue: my very simple try catch block was not working.

I found out the issue was due to newlib-nano having exceptions handling disabled by default.

In this project, I don't want to use newlib, it would simply not fit into the small flash of the LPC845 I am using.

On this post Question #230716 : Questions : GNU Arm Embedded Toolchain, I see it is possible to add support of exceptions to newlib-nano, but is this possible using MCUXpresso?

I am re using a lot of code using exceptions, I would rather spend some time adding the support to newlib nano than modify all the existing code base...

Many thanks,

Victor

Labels (1)
0 Kudos
1 Solution
2,721 Views
ErichStyger
Senior Contributor V

Exception handling is usually the most expensive feature in a language (e.g. C++ or C#). Just keep in mind or imagine what needs to be done to support exceptions.

Not only it requires special hooks on each stack frame (increasing stack size), it requires a runtime system to deal with exceptions: going through a list of descriptors, finding the matching catch block. This alone is rather complex, but gets more complex due the fact that the exception runtime needs to unwind the stack, including caring and calling all the different destructors, and continuing iterating the lists of more catch blocks and finding matching handlers. Even more, you can throw new exceptions in a catch block making things yet a bit more complex to deal with.

The exception feature is very useful and comes handy, but compared to all the other language features it is the most expensive one to me. That's why in some programming languages you can turn it off. Which might lead itself to a problem: check the case of the Ariane V rocket failure where exception handling had been turned off to meet the performance requirements.

Other than that: have a read at How is the C++ exception handling runtime implemented? - Stack Overflow  or a good video about implementation details:  YouTube 

I hope it helps,

Erich

View solution in original post

0 Kudos
6 Replies
1,031 Views
HosseinSagha
Contributor I

Use CException. It's got limitations, but better than nothing.

https://github.com/ThrowTheSwitch/CException

848 Views
HosseinSagha
Contributor I

Ahh. not really. longjmp bypasses destructors. not c++ safe.

0 Kudos
2,721 Views
converse
Senior Contributor V

AFAIK, MCUXpresso uses the standard GNU ARM distribution, so anything that is possible with that, is possible with MCUXpresso. Just download the source, and build...

However, adding Exceptions back into newlib-nano will almost turn it back into newlib - you are going to lose most (but not all, admittedly) of the size advantages of 'nano.

0 Kudos
2,721 Views
v_canoz
Contributor III

So I guess I'll add a lot of this:

#ifdef __cpp_exceptions

...

#endif

to my exisiting code.

Just wonder why exceptions takes so much room. Can a part of their functionality be disabled in order to keep the core of the exceptions?

Thanks

0 Kudos
2,722 Views
ErichStyger
Senior Contributor V

Exception handling is usually the most expensive feature in a language (e.g. C++ or C#). Just keep in mind or imagine what needs to be done to support exceptions.

Not only it requires special hooks on each stack frame (increasing stack size), it requires a runtime system to deal with exceptions: going through a list of descriptors, finding the matching catch block. This alone is rather complex, but gets more complex due the fact that the exception runtime needs to unwind the stack, including caring and calling all the different destructors, and continuing iterating the lists of more catch blocks and finding matching handlers. Even more, you can throw new exceptions in a catch block making things yet a bit more complex to deal with.

The exception feature is very useful and comes handy, but compared to all the other language features it is the most expensive one to me. That's why in some programming languages you can turn it off. Which might lead itself to a problem: check the case of the Ariane V rocket failure where exception handling had been turned off to meet the performance requirements.

Other than that: have a read at How is the C++ exception handling runtime implemented? - Stack Overflow  or a good video about implementation details:  YouTube 

I hope it helps,

Erich

0 Kudos
2,721 Views
v_canoz
Contributor III

It does, thanks !
Victor

0 Kudos