CeGCC cross compiler for PocketPC

Exception Handling with CeGCC


When an application on our target platform performs an illegal action trapped by the operating system, a predefined exception handling scenario is executed.

On a Linux or UNIX system, the process gets a signal. This behaviour is well known and documented, you can trap most signals with a signal handler. On a Windows CE device, either your application installs an exception handler, or it silently dies.

This is where our exception handling fits in. That other CE development tool is rumoured to install a default exception handler.

How to use

We've extended gcc so you can say which function is the exception handler for another function. An example :

#include <windows.h>
#include <excpt.h>

void handler(void)
        MessageBoxW(0, L"Crash !", L"Handler : error", 0);

int main(int argc, char *argv[])

int main(int argc, char *argv[])
        int     *i = 0;
        *i = 1;

The separate declaration of main basically says that exceptions that occur in main will be handled by the handler function. Note that this handler function is overly simplified for the sake of clarity.

A correctly declared handler should have four parameters, and return the correct value. A more correct and interesting example of the handler function is

int handler(struct _EXCEPTION_RECORD *ExceptionRecord,
                void *EstablisherFrame,
                struct _CONTEXT *ContextRecord,
                struct _DISPATCHER_CONTEXT *DispatcherContext)
	static wchar_t	msg[256];
	static int	i = 0;

        wsprintf(msg, L"Crash, count %d\nExc Code %x Addr %x Nparams %d\n",
        MessageBoxW(0, msg, L"WinCE Exception", 0);

Please note that use of EXCEPTION_CONTINUE_SEARCH as a return value for this function is dangerous : it should be used with extreme care, it will likely cause infinite recursion problems.

Current limitations

This technology is not mature in cegcc, we know that applications that use stdio based I/O combined with an exception handler don't work reliably.

Also no effort has been spent yet to integrate this into the arm-cegcc (formerly arm-wince-cegcc) target, only the arm-mingw32ce (formerly arm-wince-mingw32ce) target is supported.

Technical detail

Augmenting the C syntax

ARM specifics

arm-mingw32ce runtime

arm-cegcc runtime

Exception handling has not been implemented yet in the arm-cegcc runtime.

Reference material

This is not specific to Windows CE or ARM, this behaviour is documented in the reference material by ARM Ltd, see e.g. White Papers.

The Exception handling ABI for the ARM architecture contains a the most accurate description I could find.

Also several sources point to the Intel Itanium reference material which is said to be very generic in nature.

Also there are a lot of articles on forum sites which describe how to deal with exception in similar environments, usually Windows on x86 :



Powered by Sourceforge.net