Exception (try-throw-catch) handling with raw binary without run time OS support?

Is it possible to do exception handling (i.e. try – throw- catch block) on statically built raw binary, in non OS environment?

I have built a statically linked binary using GNU tools and below sample code.

// foo.cpp
#include <stdio.h>

int UkInit()
{

  int  z =0;

  try
  {
     THR_print(); 
     if(z == 0);
       throw(1);
     THR2_print();
   }
   catch (...)
   { 
      EXP_print();
   }
EXIT_print();
return 1;
}

// And compiler generated code from foo.dump is given below-
0000004e <UkInit>:
  4e:   55                      push   %ebp
  4f:   89 e5                   mov    %esp,%ebp
  51:   53                      push   %ebx
  52:   83 ec 24                sub    $0x24,%esp
  55:   c7 45 ec 0a 00 00 00    movl   $0xa,-0x14(%ebp)
  5c:   c7 45 f0 14 00 00 00    movl   $0x14,-0x10(%ebp)
  63:   c7 45 f4 00 00 00 00    movl   $0x0,-0xc(%ebp)
  6a:   e8 78 00 00 00          call   e7 <THR_print>
  6f:   c7 04 24 04 00 00 00    movl   $0x4,(%esp)
  76:   e8 85 bc 09 00          call   9bd00 <__cxa_allocate_exception>
  7b:   c7 00 01 00 00 00       movl   $0x1,(%eax)
  81:   c7 44 24 08 00 00 00    movl   $0x0,0x8(%esp)
  88:   00 
  89:   c7 44 24 04 60 e2 0a    movl   $0xae260,0x4(%esp)
  90:   00 
  91:   89 04 24                mov    %eax,(%esp)
  94:   e8 17 c0 09 00          call   9c0b0 <__cxa_throw>
  99:   89 c3                   mov    %eax,%ebx
  9b:   e8 90 c1 09 00          call   9c230 <__cxa_end_catch>
  a0:   89 d8                   mov    %ebx,%eax
  a2:   89 04 24                mov    %eax,(%esp)
  a5:   e8 46 bd 06 00          call   6bdf0 <_Unwind_Resume>
  aa:   89 04 24                mov    %eax,(%esp)
  ad:   e8 ee c0 09 00          call   9c1a0 <__cxa_begin_catch>
  b2:   e8 10 00 00 00          call   c7 <EXP_print>
  b7:   e8 74 c1 09 00          call   9c230 <__cxa_end_catch>
  bc:   b8 01 00 00 00          mov    $0x1,%eax
  c1:   83 c4 24                add    $0x24,%esp
  c4:   5b                      pop    %ebx
  c5:   5d                      pop    %ebp
  c6:   c3                      ret    

I compiled the code successfully and could see the .eh_frame and .gcc_except_table section on .dump file. Linking is OK and all needed functions are pulled in foo.elf. I generated raw binary using “objcopy -O binary foo.elf foo.bin”. I am using VMware player setup to run the raw binary.

After running the binary , i see throw prints , i.e. THR_print. I don’t see exception EXP_print. I see hang after this line from UkInit().

9bd00 <__cxa_allocate_exception> 

So my query is that –

1)- Is it possible to run this binary without run time support of OS? As i read various posts on web, section .eh_frame and .gcc_except_table are enough to handle ‘stack unwinding’ and match the ‘catch’ block.

2)- But who would do transfer of control to throw’s exception handling?

3)- Do i have to provide x86 Interrupt Descriptor Table (IDT) for x86 exception handling , and then call some function to transfer control to throw’s exception handler?

Help would be appreciated. Thanks.


Source: c++

Leave a Reply