GCC C++ Exception Handling Implementation

后端 未结 2 1043
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 02:13

I would like to know how GCC implements exception handling for C++ programs. I couldn\'t find an easy-to-understand and self-explanatory article on the Web (although there are m

2条回答
  •  星月不相逢
    2021-02-06 02:40

    The Itanium ABI (which both gcc, clang and a number of others follow) specify that exception handling should follow the Zero-Cost strategy.

    The idea of the Zero-Cost strategy is to push all exception handling in side-tables that are not kept on the main program execution path (and thus not trashing the instruction cache). These tables are indexed by the program point.

    Furthermore, DWARF information (which is debug information really) is used to unwind the stack. This functionality is usually provided as a library such as libunwind for example, the source code is chokeful of assembly (and thus very platform specific).

    Advantages:

    • 0-cost for entering try/catch block (as fast as if there was none)
    • 0-cost for having a throw statement in a function (as long as it is not taken)

    Disadvantage:

    • Slow in case of exception (10x slower than an if strategy) because the side tables are usually not in cache and then there are expensive computations to run to know which catch clause actually matches (based on RTTI)

    It is a very popular strategy implement on both 32 bits and 64 bits platform for all major compilers... except MSVC 32 bits (if I remember correctly).

提交回复
热议问题