GCC异常处理模型 [英] Exception handling models of GCC

查看:510
本文介绍了GCC异常处理模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC支持Setjump-longjump(sjlj)和Dwarf2基于表的解卷(dw2)异常处理模型。两种机型有什么区别?如何选择合适的型号?为什么Dwarf2基于表的展开(dw2)更有效的模式?我明白这两个模型不能混合。

GCC supports Setjump-longjump (sjlj) and Dwarf2 table-based unwinding (dw2) exception handling models. What is the difference between the two models and how to choose the appropriate model? Why is Dwarf2 table-based unwinding (dw2) the more efficient model? I understand that the two models cannot be mixed.

参考:技术预览:gcc-4.2.1-sjlj -2

推荐答案

嗯,dwarf2为每个函数建立表,其中包含被调用者保存的寄存器以及保存在堆栈中的位置,调用堆栈中的帧指针/返回地址以及其他一些内容。如果使用dwarf2,编译器可以使用这些信息并有效地恢复寄存器,并在发生异常时跳回到调用者。后端需要在其实现的序幕生成代码中提供信息,告诉GCC哪些寄存器被保留,并且框架指针被保存和这样的东西。

Well, dwarf2 builds up tables for every function, that contain what the callee saved registers are and where in the stack they are saved, and where the frame pointer/return address in the callstack is, and some other stuff. If you use dwarf2, the compiler can use those information and effectively restore registers, and jump back to callers in the event of an exception. The backends need to provide information in their implementations' prologue generating code, to tell GCC which registers are callee-saved, and when the frame pointer was saved and such stuff.

使用setjmp / longjmp只是一个黑客。由于setjmp / longjmp不知道函数抛出的结构,所以即使它们没有被抛出函数覆盖,它还可以通过setjmp来还原保存在跳转缓冲区中的所有寄存器。我并不是一个专家,但我认为这显然不是很有效率。此外,每次启动一个try块时,必须调用setjmp来设置包含保存的寄存器的缓冲区,而在使用dwarf2时,编译器已经在编译时提供了所有必要的信息。

Using setjmp/longjmp is just a hack. Since setjmp/longjmp does not know about the structure of the function throwing, it will restore all registers saved in the jump-buffer by setjmp, even if they were not overridden by the throwing function. I'm not really an expert for this, but i think it's obvious that this will not be efficient. Also, every time you start a try block, setjmp has to be called to set up the buffer containing the saved registers, while when using dwarf2, the compiler already provides all necessary information at compile time.

如果后端没有提供必要的信息,GCC将自动回退到基于setjmp / longjmp的异常处理。

If the backends do not provide the necessary information, GCC will automatically fall-back to setjmp/longjmp based exception handling.

注意我不是海湾合作委员会专家。我刚把工具链移交给我的教授的一些简单的处理器,包括GCC。我希望我可以帮你一些。

Note i'm not a GCC expert. I just ported the toolchain to some easy processor of my professor, including GCC. I hope i could help you a bit.

这篇关于GCC异常处理模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆