Exception handling porting inGCCShiva Chenshiva0217@gmail.comMAY 2013
Why need exception handling ? Exceptions provide a way to react toexceptional circumstances (like runtimeerrors) Transfe...
Example// exceptions#include <iostream>using namespace std;intmain (){try { throw 20; }catch (int e){cout << "An exception...
Unwinding After throwing a exception object, exception library will dounwinding frame to find the handler which could han...
Dwarf2 unwinding Startup code to support dwarf2 unwinding_initcall frame_dummycall __register_frame_info_basecall __do_gl...
Dwarf2 unwinding relative porting There are three thing for Dwarf2 unwinding areABI specific which need porting code to h...
Dwarf2 unwinding relative porting Pass exception info to exception handler Define EH_RETURN_DATA_REGNOThe register to p...
eh_return pattern of armarm_set_return_address (rtx source, rtx scratch){…/* if return address in LR_REGNUMemit a move to ...
eh_return pattern of armarm_set_return_address (rtx source, rtx scratch){…/* return value in stack */else{if (frame_pointe...
Sections for dwarf2 unwinding .eh_frame Contains the information necessary to pop back to thestate of the machine regist...
Reference C++ ABI for Itanium: Exception handling http://refspecs.linuxfoundation.org/abi-eh-1.22.htm sjlj vs dw2 in GC...
Upcoming SlideShare
Loading in …5
×

Exception handling poirting in gcc

338
-1

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
338
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Exception handling poirting in gcc

  1. 1. Exception handling porting inGCCShiva Chenshiva0217@gmail.comMAY 2013
  2. 2. Why need exception handling ? Exceptions provide a way to react toexceptional circumstances (like runtimeerrors) Transferring control to special functions calledhandlers Make system more stable
  3. 3. Example// exceptions#include <iostream>using namespace std;intmain (){try { throw 20; }catch (int e){cout << "An exception occurred. Exception Nr. " << e << endl;}return 0;}
  4. 4. Unwinding After throwing a exception object, exception library will dounwinding frame to find the handler which could handle theexception object. There are two way to do unwinding Set jump/long jump (SJLJ) unwinding Don’t know about the frame info restore all registers saved in the jump-buffer by setjmp Smaller code size but slower (store all register when enter each frame) Configure gcc with --enable-sjlj-exceptions Dwarf2 unwinding Obtain eh_rame, gcc_except_table to record frame relative info Could know which register will really corrupt and restore the register whichreally needed Code size bigger but faster
  5. 5. Dwarf2 unwinding Startup code to support dwarf2 unwinding_initcall frame_dummycall __register_frame_info_basecall __do_global_ctorscall atexit (__do_global_dtors)__register_frame_info_baseCalled from crtbegin.o to register the unwind info for an objectlibgcc/config.hostextra_parts="crti.o crtn.o crtbegin.o crtend.o crtbeginS.o crtendS.o“Which will use libgcc/crtstuff.c to build startup code
  6. 6. Dwarf2 unwinding relative porting There are three thing for Dwarf2 unwinding areABI specific which need porting code to handlethis part Pass exception info to exception handlerPrologue need preserve original register value Adjust stack pointer from unwind frame to handler Modify return value to exception handlerAfter unwinding, unwinding library will modify return value tohandler and jump to handler by function returnReturn value could be in Return register Frame stack
  7. 7. Dwarf2 unwinding relative porting Pass exception info to exception handler Define EH_RETURN_DATA_REGNOThe register to pass exception infoPrologue need push original value to the stack before settingexception info Adjust stack pointer from unwind frame to handler Define EH_RETURN_STACKADJ_RTXThe register to contain adjust offset Modify return value to exception handler Define EH_RETURN_HANDLER_RTX or eh_returnpatternTo modify return value to handler
  8. 8. eh_return pattern of armarm_set_return_address (rtx source, rtx scratch){…/* if return address in LR_REGNUMemit a move to change return address to handler */if ((saved_regs & (1 << LR_REGNUM)) == 0)emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);/* return value in stack */else{}source contain return value
  9. 9. eh_return pattern of armarm_set_return_address (rtx source, rtx scratch){…/* return value in stack */else{if (frame_pointer_needed)addr = plus_constant(hard_frame_pointer_rtx, -4);else{ /* LR will be the first saved register. */delta = offsets->outgoing_args - (offsets->frame + 4);If (delta >= 4096){emit_insn (gen_addsi3 (scratch, stack_pointer_rtx, GEN_INT (delta & ~4095)));addr = scratch; delta &= 4095; }elseaddr = stack_pointer_rtx;addr = plus_constant (addr, delta);}emit_move_insn (gen_frame_mem (Pmode, addr), source);}}source contain return valueUse fp to find return address in stackEmit memory move to modify return address in stackFind the return address in stack
  10. 10. Sections for dwarf2 unwinding .eh_frame Contains the information necessary to pop back to thestate of the machine registers and the stack at any pointhigher up the call stack .gcc_except_table Contains information about the exception handling"langing pads" the locations of handlers. This is necessary so as to know when to stopunwinding. http://gcc.gnu.org/ml/gcc-help/2010-09/msg00116.html
  11. 11. Reference C++ ABI for Itanium: Exception handling http://refspecs.linuxfoundation.org/abi-eh-1.22.htm sjlj vs dw2 in GCC http://stackoverflow.com/questions/318383/exception-handling-models-of-gcc
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×