newlib/libgloss/arm/_kill.c
Matthew Malcomson 6dbd190111 Enable return code with semi-hosting SYS_EXIT_EXTENDED
The _exit function currently passes -1 as a "sig" to the _kill function as an
invalid signal number so that _kill can distinguish between an abort and a
standard exit.

For boards using the SYS_EXIT_EXTENDED semi-hosting operation to return a
status code, this means that the "status" paramter to _exit is ignored and the
return code is always -1.
https://developer.arm.com/docs/100863/latest/semihosting-operations/sys_exit_extended-0x20

This patch puts shared code between _kill and _exit into a new function
_kill_shared that takes the semi-hosting "reason" to use (if semi-hosting is
available) as an argument.

For semi-hosting _kill_shared provides that "reason".

Without the "sig" argument being used to distinguish between a normal and
abnormal exit, the _exit function can provide the return code to be used if the
SYS_EXIT_EXTENDED operation is available.

Hence the exit code can be returned.
2018-11-19 13:46:19 +01:00

52 lines
931 B
C

#include <_ansi.h>
#include <signal.h>
#include "swi.h"
int _kill_shared (int, int, int) __attribute__((__noreturn__));
int _kill (int, int);
int
_kill (int pid, int sig)
{
if (sig == SIGABRT)
_kill_shared (pid, sig, ADP_Stopped_RunTimeError);
else
_kill_shared (pid, sig, ADP_Stopped_ApplicationExit);
}
int
_kill_shared (int pid, int sig, int reason)
{
(void) pid; (void) sig;
#ifdef ARM_RDI_MONITOR
/* Note: The pid argument is thrown away. */
int block[2];
block[1] = sig;
block[0] = reason;
int insn;
#if SEMIHOST_V2
if (_has_ext_exit_extended ())
{
insn = AngelSWI_Reason_ReportExceptionExtended;
}
else
#endif
{
insn = AngelSWI_Reason_ReportException;
}
#if SEMIHOST_V2
if (_has_ext_exit_extended ())
do_AngelSWI (insn, block);
else
#endif
do_AngelSWI (insn, (void*)block[0]);
#else
asm ("swi %a0" :: "i" (SWI_Exit));
#endif
__builtin_unreachable();
}