Adding the -O2 option when cross-compiling causes the unwind backtrace to fail

278 Views Asked by At

Add -funwind-tables when cross-compiling, you can successfully unwind backtrace through the interface(_Unwind_Backtrace and _Unwind_VRS_Get) in the libgcc library. But when I added the -O2 option at cross-compiling time, unwind backtrace would fail. I pass -Q -O2 --help=optimizers print out the optimization and testing, but the results and -O2 is different, very strange,

1

There are 1 best solutions below

6
On

You haven't told us which ARM architecture you are building for - but assuming it's a 32-bit architecture, enabling -O2 has also enabled -fomit-frame-pointer-fomit-frame-pointer)

The frame-pointer

The frame pointer contains the base of the current function's stack frame (and with the knowledge that the caller's frame pointer is stored on the stack, a linked list of all stack frames in the call-tree). It's usually described as fp in documentation - a synonym for r11.

Omitting the frame-pointer

The ARM register file is small at 16 registers - one of which is the program counter. The frame pointer is one of the 15 that remains and is used only for debugging and diagnostics - specifically to provide stack walk-backs and symbolic debugging.

-fomit-frame-pointer tells the compiler to not maintain a frame pointer, thus liberating the r11 for other uses, and potentially avoiding spill of variables to the stack from registers. It also saves 4 bytes per stack-frame of stack storage and a store and load to the stack.

Naturally, if fp is used as general purpose register, its contents are undefined and walk-backs won't work.

You probably want to reenable the frame pointer with -fno-omit-frame-pointer for your own sanity.