I am currently playing around with embedded C++ programming, I am trying to understand the C HAL provided by ST, rework it in modern C++, optimize some stuff away, etc.
I got to the point where I can blink an LED. The size of my current binary is 1800 bytes, but along the way, it occurred more than once that my binary size grew significantly because of very small changes. Significant means that I got an 6144 bytes binary instead of my 1800 bytes one, which is more than a 3x increase. My goal is of course to keep the binary size small.
Every time that this happened, I looked into the disassembled elf file and I noticed that the compiler included some kind of stack unwinding stuff, and that is what caused the increase of the binary size. In the disassembled file, I can see symbols like __gnu_unwind_get_pr_addr
, __gnu_Unwind_Restore_VFP
, _Unwind_DebugHook
, __gnu_Unwind_RaiseException
, etc.
First time this happened, I was using some shady pointer stuff, so I can understand that because of that some exception could happend and then this stack unwinding stuff is needed. Last time I copied over a function and forgot to remove the __attribute__((weak))
attribute. Even though I found that this was causing it, I don't really see what the problem is there, I thought that if I don't implement a strong function, then the weak one will be used without any problems.
Question: what exactly is this unwinding stuff that gets added to the binary, what is its purpose, and what causes it generally speaking?
My assumption is that if I write code that could potentially cause an exception to happen, then stack unwinding is a necessary component of the exception mechanism, so that is why this extra code is added. But I am just guessing here, it is hard to find anything about this online.