且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

gcc如何在linux上实现C ++异常的栈展开?

更新时间:2023-11-13 13:05:10

请参阅 x86_64 ABI 的第6.2节。这详细说明了接口,但不是很多底层数据。这也是独立于C ++,并且可以想象也用于其他目的。

See section 6.2 of the x86_64 ABI. This details the interface but not a lot of the underlying data. This is also independent of C++ and could conceivably be used for other purposes as well.

主要有两个ELF二进制文件由gcc发出,这是感兴趣的异常处理。它们是 .eh_frame .gcc_except_table

There are primarily two sections of the ELF binary as emitted by gcc which are of interest for exception handling. They are .eh_frame and .gcc_except_table.

.eh_frame 遵循DWARF格式(当您使用gdb时主要发挥的调试格式) 。它与使用 -g 编译时发出的 .debug_frame 节完全相同的格式。本质上,它包含弹回到机器寄存器和堆栈在调用堆栈上的任何点的状态所需的信息。有关此问题的详细信息,请参阅dwarfstd.org上的Dwarf Standard。

.eh_frame follows the DWARF format (the debugging format that primarily comes into play when you're using gdb). It has exactly the same format as the .debug_frame section emitted when compiling with -g. Essentially, it contains the information necessary to pop back to the state of the machine registers and the stack at any point higher up the call stack. See the Dwarf Standard at dwarfstd.org for more information on this.

.gcc_except_table 包含有关异常处理的信息着陆垫处理程序的位置。这是必要的,以便知道何时停止展开。不幸的是,这一部分没有很好的文档。我已经能够收集的信息的唯一片段来自gcc邮件列表。特别参见此信息

.gcc_except_table contains information about the exception handling "landing pads" the locations of handlers. This is necessary so as to know when to stop unwinding. Unfortunately this section is not well documented. The only snippets of information I have been able to glean come from the gcc mailing list. See particularly this post

剩下的信息就是实际代码解释在这些数据段中找到的信息。相关代码存放在libstdc ++和libgcc中。我不记得在哪个片段住在哪里。 DWARF调用帧信息的解释器可以在文件gcc / unwind-dw.c中的gcc源代码中找到。

The remaining piece of information is then what actual code interprets the information found in these data sections. The relevant code lives in libstdc++ and libgcc. I cannot remember at the moment which pieces live in which. The interpreter for the DWARF call frame information can be found in the gcc source code in the file gcc/unwind-dw.c