1 /*
2                     Language Independent Library
3 
4             Copyright 2006-2012 Green Hills Software,Inc.
5 
6  *  This program is the property of Green Hills Software, Inc,
7  *  its contents are proprietary information and no part of it
8  *  is to be disclosed to anyone except employees of Green Hills
9  *  Software, Inc., or as agreed in writing signed by the President
10  *  of Green Hills Software, Inc.
11 */
12 
13 /* This module is meant to perform stack checking using the -stack_check
14  * option.
15  */
16 
17 /* Get __get_stack_pointer() on appropriate targets */
18 #include <alloca.h>
19 #include "ind_io.h"  /* write() */
20 #include <stdlib.h> /* exit() */
21 #include <string.h> /* strlen() */
22 #include <cafe/os.h>
23 
24 #pragma ghs startnoinline
25 /* CAFE MOD */
emit_stack_error(const char * tname)26 static void emit_stack_error(const char *tname)
27 {
28     static const char err[] = "Stack overflow error in \"";
29     (void)write(1, err, sizeof(err)-1);
30     (void)write(1, tname, strlen(tname));
31     (void)write(1, "\"\n", 2);
32     OSDebug();
33     /* exit is already pulled in by ind_crt1.c, so use it */
34     exit(1);
35 }
36 
__stkchk(void)37 void __stkchk(void)
38 {
39     static int did_error = 0;
40 #ifdef __GHS_TARGET_IMPLEMENTS_ALLOCA
41     void *sp = __get_stack_pointer();
42 #else
43     char loc;
44     void *sp = (void *)&loc;
45 #endif /* __GHS_TARGET_IMPLEMENTS_ALLOCA */
46     OSThread* thread;
47     if (!did_error) {
48 	thread = OSGetCurrentThread();
49 	if(sp <= (void *)thread->stackEnd) {
50 	    did_error = 1;
51 	    emit_stack_error(thread->name);
52 	}
53     }
54 }
55