Bug 28045 - add --warn-uncaught-exceptions to linker
Summary: add --warn-uncaught-exceptions to linker
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-01 19:59 UTC by Ben Woodard
Modified: 2023-08-07 09:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Woodard 2021-07-01 19:59:48 UTC
Right now given some random library it is very difficult and requires an awful amount of digging to just figure out what you should try to catch. It would be great if the linker which can observe these things as it is linking the binary could point out uncaught exceptions.

So when gcc links the developer could pass:
-Wl,--warn-uncaught-exceptions

Then for every type thrown as listed in the LSDA, if at least one of the libraries doesn't have an entry in the TType table then it prints something like:

Warning: thus_and_such_function throws type ExceptionType at line N there is no corresponding catch block that can catch that type.

Then when they see that they could look at their code that either directly or indirectly calls the named function and insert a catch block. And like most warnings vs. errors they can look at it and say — yeah I don’t really care and ignore it.

The simplest blunt way to fix that warning would be wrap the contents of main in a try block and put catch blocks for the types being pointed out there. It obviously would be better to be more targeted in where they insert their catch blocks.

This would just be a first approximation, it wouldn't necessarily be able to guarantee that there isn't a way to reach some code where an exception wouldn't be caught. To be able to do that, you would have to do call chain analysis. However, it would insure that somewhere in the totality of the program being linked there is a catch block that can handle that type.
Comment 1 Ben Woodard 2021-07-01 20:29:44 UTC
Some additional things to consider:
https://wiki.c2.com/?DontCatchExceptions

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Re-catch
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e17-dont-try-to-catch-every-exception-in-every-function

All of these basically end up dealing with where to try to catch exceptions vs whether all the different types of exceptions are caught.