[HN Gopher] C++ Exceptions: Under the Hood
       ___________________________________________________________________
        
       C++ Exceptions: Under the Hood
        
       Author : arcatek
       Score  : 20 points
       Date   : 2021-08-12 21:17 UTC (1 hours ago)
        
 (HTM) web link (monkeywritescode.blogspot.com)
 (TXT) w3m dump (monkeywritescode.blogspot.com)
        
       | AshamedCaptain wrote:
       | [2013]ish if I remember.
       | 
       | Also note the ABI for C++ exceptions followed by G++ et al is
       | actually documented as part of the Itanium C++ ABI :
       | 
       | https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
        
         | gpderetta wrote:
         | The doc is actually incomplete, it refers to implementation
         | defined ABI entry points for the actual unwinding. The actual
         | unwind tables and compensation opcodes are, IIRC, part of the
         | DWARF standard, but last time I looked at it that standard was
         | also incomplete and left a lot unspecified. Many of the details
         | (including bugs that have now become part of the ABI) are
         | basically folklore.
         | 
         | IIRC Ian LAnce Taylor had a series of blog posts that did shed
         | light on a lot of these details.
        
           | jcranmer wrote:
           | There's essentially three separate moving pieces here.
           | 
           | At the bottom layer you have the unwind API. This is actually
           | generally defined by the platform-specific ABI, although the
           | definition here on most Unixes is "we have a .eh_frame
           | section that's basically the same as .debug_frame in DWARF."
           | The API is provided by some platform library (libunwind), and
           | it's language-agnostic.
           | 
           | Part of the generic unwind structure is that each stack frame
           | has a personality function and a Language-Specific Data Area,
           | and the unwind semantics will call the personality function
           | to figure out whether to drop off into the stack frame, and
           | where in the function to drop off, or continue unwinding the
           | stack. The personality function itself uses the LSDA to
           | figure out what to do. The personality function lives in the
           | language standard library (e.g., libstdc++), and the LSDA
           | format is of course entirely undocumented (i.e., read Ian
           | Lance Taylor's blog posts as the best documentation that
           | exists).
           | 
           | The final level of the ABI is how you actually represent the
           | exceptions themselves. This is provided by the Itanium ABI
           | and describes the names of the functions needed to effect
           | exception handling (e.g., __cxa_begin_catch), the structure
           | layouts involved, and even some nominal data on how to handle
           | foreign exceptions which don't really exist.
           | 
           | And that's not entirely true for all systems. ARM notably
           | uses a different ABI exception handling, which is rather
           | close to the standard Itanium exception handling except the
           | details are different. Some operating systems choose to forgo
           | unwinding-based exceptions in lieu of setjmp/longjmp as the
           | standard ABI, which of course requires different versions of
           | everything. And Windows has an entirely different form of
           | exception handling which isn't centered around unwinding but
           | actually calling exception handlers as new functions on the
           | stack, requiring frame links to the original-would-be-
           | unwound-to function.
        
       | zabzonk wrote:
       | Under the hood of GCC specifically.
        
         | arcatek wrote:
         | That's the article I used when I implemented exceptions in an
         | LLVM-based compiler, so it's applicable to more than just GCC.
        
         | cogman10 wrote:
         | Doesn't GCC support multiple exception handling options?
        
           | zabzonk wrote:
           | I'm not sure what your point is - mine was that the original
           | post is specifically about GCC, not Standard C++.
        
             | [deleted]
        
             | cogman10 wrote:
             | Mainly that exception handling doesn't have a single
             | solution, even in GCC. I'm agreeing with you that this
             | article only highlights one version of exception handling
             | for one compiler.
             | 
             | There's also potential operating system involvement that's
             | not really covered in this article.
        
           | mhh__ wrote:
           | I'm not sure exactly what you mean but there was a switch to
           | DWARF EH ages ago (GCC 2?)
        
       ___________________________________________________________________
       (page generated 2021-08-12 23:00 UTC)