0

Edit: this problem is not with my code, it has been tested on numerous other problems without issue. This is a gcc cross compilation problem.

I am cross compiling a large C++ program with g++ on Ubuntu x64 to run on a Raspberry Pi 2. When using gcc-4.6.4 everything seems to work. When using gcc-4.8.5, exceptions seems to be leaking through and causing the program to abort. I have tried a minimal example of exception catching using gcc-4.8.5 and the minimal case does seem to work properly. My actual program is far more complicated and it seems the exception catch is being lost somewhere.

Is there some g++ setting that I am missing that will improve exception handling?

I am compiling the toolchains with crosstools-ng 1.22

Edit: This is essentially what the code is doing:

//error_function may be deeper in the stack
void error_function()
{
    throw std::runtime_error("This is an error");
}

try
{
    error_function();
}
catch (std::exception&)
{
   //Not being caught
}
John
  • 791
  • 1
  • 6
  • 22
  • Could it be that you are throwing an exception from a C++ callback called from a C library? – rodrigo Apr 02 '16 at 19:32
  • I'm relatively sure the problem is somewhere in your code. Extract a minimal example. – Ulrich Eckhardt Apr 02 '16 at 19:33
  • This code has been extensively tested on multiple platforms without issue. The problem only occurs on gcc-4.8 armhf. I haven't been able reproduce the problem with a minimal example, and the code is proprietary. – John Apr 02 '16 at 19:34
  • Start trimming your code until you get down to an example you can share. Very unlikely anyone will be able to help otherwise. – Nemo Apr 02 '16 at 19:41
  • Trimming my code would not lead to a functional example. I have added more of an example. The problem seems to be because the catch is within a static library while the function is part of the application. This seems to be a problem with the gcc configuration. The problem is not with my software. – John Apr 02 '16 at 19:48
  • @John We don't need a functional example. We need an example where an exception should work but does not. Whether the code does anything useful or not is irrelevant to the question. – rodrigo Apr 02 '16 at 19:51
  • I did a complete rebuild of the 4.8.5 toolchain and it seems to work now. I am still not sure what was wrong because I did not change any settings in the toolchain. – John Apr 02 '16 at 23:09
  • Is the static library compiled with the same compiler? With the same compiler options? I.e. both C++, same optimisation options and so forth? Is the function declared as extern "C"? It is possible that you are the victim of and... overenthusiastic ... optimisation. – Ben Apr 03 '16 at 13:45
  • Yes, it is compiled at the same time. And yes, I set -O0 and everything works. Debug build works now as well. – John Apr 04 '16 at 20:07

1 Answers1

1

Turning off optimization by setting -O0 seems to fix the problem. This bug seems to be specific to gcc 4.8.5 ARMv6 hard float build.

John
  • 791
  • 1
  • 6
  • 22
  • You should bisect which *specific* `-f` flag is the problem using commands like `gcc -c -Q -O0 --help=optimizers`. In particular, I would try looking at options relating to unwind tables. – o11c Apr 04 '16 at 20:15
  • I am unfortunately not familiar with this level of compiler details. I have created a paste-bin comparing gcc -c -Q -O0 --help=optimizers and gcc -c -Q -O3 --help=optimizers. Do you have any suggestions? Testing each switch would take quite a long time. http://pastebin.com/fMV8RhTv – John Apr 05 '16 at 01:14
  • you don't have to test all the options at *once*, you can test half of them at a time, in `O(log(n))` time. That's why I said "bisect". – o11c Apr 05 '16 at 01:33
  • That assumes that a single option is causing the problem rather than a combination. Which optimizations are most likely to cause the problem? – John Apr 05 '16 at 01:38