How is an exception transferred to find a handler?

前端 未结 2 1107
[愿得一人]
[愿得一人] 2021-01-24 06:13

When an exception is thrown stack unwinding is initiated until handling code is encountered, but I am a little unclear on the mechanics of the whole process.

1 - where i

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 06:37

    Source: How do exceptions work (behind the scenes) in c++ (I read the assembly and answered the questions by what I understood)

    Question 1#:

    movl    $1, (%esp)
    call    __cxa_allocate_exception
    movl    $_ZN11MyExceptionD1Ev, 8(%esp)
    movl    $_ZTI11MyException, 4(%esp)
    

    _ZTI11MyException is the exception. It looks as if it has it's own allocation not in the stack and it places the pointer in register named eax.

    Question 2#:

    .LFE9:
    .size   _Z20my_catching_functionv, .-_Z20my_catching_functionv
    .section    .gcc_except_table,"a",@progbits
    .align 4
    

    It looks like table that is stored in static data in the program. So it can know where it can catch. There was nothing about how objects destruct themself after unwinding frames so this is from Visual Studio: (The link at the top is from Linux)

            MyClass s, s2, s3, s4;
     mov         dword ptr [ebp-4],3  
            try {
                {
                    MyClass s, s2, s3, s4;
     mov         byte ptr [ebp-4],7  
                }
    

    It looks like it saves the number of objects to destroy. For example when it finishes:

    call        MyClass::~MyClass (0DC1163h)
    mov         dword ptr [ebp-4],0FFFFFFFFh
    

    0FFFFFFFFh means nothing's to destruct. If I find something about how it actually finds and destroyes them I will add here.

    Question 3#:

    As in the previous question, you see there's table for it, it can know whatever it's in the right function.

提交回复
热议问题