C++ Exception Support

From OSDev.wiki
Revision as of 02:45, 12 July 2011 by osdev>Torshie (New page: Under Linux, G++ implements the [http://www.codesourcery.com/public/cxx-abi/abi-eh.html Itanium C++ ABI]. If your kernel is compiled with a Linux compiler (either cross compiler or native ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Under Linux, G++ implements the Itanium C++ ABI. If your kernel is compiled with a Linux compiler (either cross compiler or native compiler), you can follow this article and add C++ exception support to your kernel.

Introduction

In the ABI, C++ exception is supported by the cooperation of three layers. The first layer is the compiler. The compiler translates the "try" "catch" "throw" statements into calls to specific functions in C++ runtime. The second layer is the C++ runtime. This layer is pretty thin, it is simply a cross-platform wrapper around the third layer. All the dirty work are done by the third layer: the unwind library.

So, in order to add C++ exception support, you just need to port the second and the third layer into you kernel, which means you need to either port libsupc++ or port libcxxrt and an unwind library. I prefer the second choice, because libsupc++ is part of GCC, it even not trivial to compile it separately. For the unwind library part, you have two choices, libunwind and libgcc_eh. libunwind is more difficult to port, it needs pthread and some type definitions in elf.h link.h ucontext.h. The major reason you may want libunwind instead of libgcc_eh is that libgcc_eh is licensed under LGPL.

Port libcxxrt

libcxxrt can be downloaded from [1]. libcxxrt is very easy to port, only 1 .c file and 8 .cc files. It depends on a few simple libc functions and a few pthread functions. If you don't need multi-thread support in your kernel, create some pthread stubs. Then copy all the source files and header files into your own kernel source tree and compile them with your existing build system, no special compiler flags are needed.

Port libgcc_eh

libgcc_eh is more difficult