Division by zero in _exit of libnosys

Jie Zhang jzhang918@gmail.com
Sun Feb 11 05:26:00 GMT 2007


Hi,

In libnosys of libgloss, the default stub of _exit ():

_VOID
_DEFUN (_exit, (rc),
	int rc)
{
  /* Default stub just causes a divide by 0 exception.  */
  int x = rc / INT_MAX;
  x = 4 / x;

  /* Convince GCC that this function never returns.  */
  for (;;)
    ;
}

tries to cause a divide-by-zero exception. Division by 0 is an
undefined behavior in C. It's not guaranteed to generate the
exception. For this piece of code, gcc will optimize away the
dividsions, leaving only the empty loop.

We have to find another way to do that. How about the below method?

#ifdef __BFIN__
  asm ("excpt 0;");
#else
  /* Default stub just causes a divide by 0 exception.  */
  int x = rc / INT_MAX;
  x = 4 / x;
#endif

Or is there an easy way to define a real _exit for each port to
override the stub?


Jie



More information about the Newlib mailing list