1 /*
2 		    ISO C Runtime Library
3 
4 	Copyright 1983-2009 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 #include "inddef.h"
14 #include "ind_crt1.h"
15 #include "ind_exit.h"
16 #include "ind_thrd.h"
17 
18 void exit(int);
19 int atexit(void (*function)(void));
20 #if defined (__CRT_TRACE_INIT)
21 extern void __ghs_trace_init(void);
22 #endif
23 #pragma weak __ghs_board_devices_init
24 extern void __ghs_board_devices_init(void);
25 #pragma weak __gh_iob_init
26 extern void __gh_iob_init(void);
27 #pragma weak __gh_lock_init
28 extern void __gh_lock_init(void);
29 #pragma weak __cpp_exception_init
30 extern void __cpp_exception_init(void **);
31 #pragma weak __ghs_cpp_exception_init
32 extern void __ghs_cpp_exception_init(void);
33 #pragma weak __ghs_manprf_init
34 extern void __ghs_manprf_init(void);
35 #if defined(__mips) && !defined(__TRW_RH32__) && !defined(__MIPSX__)
36 int __ghs_start_pmon_profiling(void);
37 #endif
38 #if defined(__StarCore__)
39 extern void __destroy_global_chain(void);
40 extern void __exec_staticinit(void);
41 #endif
42     /* Declare 'main' as a far function on MIPS when in LocalPIC mode,
43        so that it can be reached however large the user program is.
44        Without being 'far', 'main' can be out of the range of 16-bits
45        that a local pic call allows when the user program is too large. */
46 #if defined(__mips) && !defined(__TRW_RH32) && !defined(__MIPS_X) && \
47 	defined(__ghs_pic) && defined(__localpic)
48 #pragma ghs callmode=far
49 extern 	int main(int argc, char ** argv, char **envp);
50 #pragma ghs callmode=default
51 #else
52 extern	int main (int argc, char **argv, char **envp);
53 #endif
54 
55 
56 /*----------------------------------------------------------------------*/
57 /* ind_crt1.c: Machine Independent library initialization startup code. */
58 /*									*/
59 /* CALLED FROM:  __ghs_ind_crt0 in ind_crt0.p				*/
60 /* ENTRY POINT:  __ghs_ind_crt1 					*/
61 /*----------------------------------------------------------------------*/
62 /* This is the first C function called by the standard libraries after	*/
63 /* memory and static and global data have been initialized.  It         */
64 /* initializes the ANSI C and other run time libraries before jumping   */
65 /* to the application entry point (main).				*/
66 /* Arguments to the initialization routine:  argc, argv, and envp.	*/
67 /* These are valid if the program is run from the debugger or 0 if not.	*/
68 /* If argc==0, we construct the arguments to main() on our stack frame. */
69 /* You may change these arguments to provide any other default values	*/
70 /*----------------------------------------------------------------------*/
71 
72 #if defined(RODATA_IS_INDEPENDENT)
73 #  define CONST_FUNCP *
74 #else
75 #  define CONST_FUNCP *const
76 #endif
77 
78 char **environ;
79 
80 #ifdef __mips
81 #pragma ghs far
82 #endif
83 
84 #if 1 // CAFE MOD
__ghs_ind_crt1(int argc,char * argv[],char * envp[])85 void __ghs_ind_crt1 (int argc, char *argv[], char *envp[])
86 {
87     /* Hold the default arguments to main() when they are not passed
88      * to the target by the debugger.
89      */
90 
91 #if defined (__CRT_TRACE_INIT)
92 /*------------------*/
93 /* initialize trace */
94 /*------------------*/
95     {
96 	__ghs_trace_init();
97     }
98 #endif
99 
100 /*--------------------------*/
101 /* initialize board devices */
102 /*--------------------------*/
103     {
104 #if defined(__ghs_pic)
105 	static void (CONST_FUNCP board_devices_init_funcp)(void) =
106 	    __ghs_board_devices_init;
107 	if (board_devices_init_funcp)
108 #else  /* defined(__ghs_pic) */
109 	if (__ghs_board_devices_init)
110 #endif /* defined(__ghs_pic) */
111 	__ghs_board_devices_init();
112     }
113 
114 /*-----------------*/
115 /* initialize lock */
116 /*-----------------*/
117     {
118 #if defined(__ghs_pic)
119 	static void (CONST_FUNCP lock_init_funcp)(void) = __gh_lock_init;
120 	/* If ind_thrd.c is loaded, initialize the C library lock */
121 	if (lock_init_funcp)
122 #else  /* defined(__ghs_pic) */
123 	if (__gh_lock_init)
124 #endif /* defined(__ghs_pic) */
125 	 __gh_lock_init();
126     }
127 
128 /*----------------*/
129 /* initialize iob */
130 /*----------------*/
131     {
132 #if defined(__ghs_pic)
133 	static void (CONST_FUNCP iob_init_funcp)(void) = __gh_iob_init;
134 	/* if ind_iob.c is loaded, initialize _iob for stdin,stdout,stderr */
135 	if (iob_init_funcp)
136 #else  /* defined(__ghs_pic) */
137 	if (__gh_iob_init)
138 #endif /* defined(__ghs_pic) */
139 	 __gh_iob_init();
140     }
141 
142 #if !defined(__disable_thread_safe_extensions) && !defined(__OSE)
143 /*--------------------*/
144 /* C++ Thread-safe    */
145 /* Exception handling */
146 /*--------------------*/
147     {
148 #if defined(__ghs_pic)
149 	static void (CONST_FUNCP cpp_init_funcp)(void **) = __cpp_exception_init;
150 	if (cpp_init_funcp)
151 #else  /* defined(__ghs_pic) */
152 	if (__cpp_exception_init)
153 #endif /* defined(__ghs_pic) */
154 	    __ghs_cpp_exception_init();
155     }
156 #endif /* !defined(__disable_thread_safe_extensions) && !defined(__OSE) */
157 
158 /*-----------------------------*/
159 /* initialize manual profiling */
160 /*-----------------------------*/
161     {
162 #if defined(__ghs_pic)
163 	static void (CONST_FUNCP man_prf_funcp)(void) = __ghs_manprf_init;
164 	/* if ind_manprf.c is loaded, initialize manual profiling */
165 	if (man_prf_funcp)
166 #else  /* defined(__ghs_pic) */
167 	if (__ghs_manprf_init)
168 #endif /* defined(__ghs_pic) */
169 	__ghs_manprf_init();
170     }
171 
172     // K2 : truncates for cafe
173 }
174 #endif // CAFE MOD
175 
176 #if 0 // CAFE GHS ORIG
177 void __ghs_ind_crt1 (int argc, char *argv[], char *envp[])
178 {
179     /* Hold the default arguments to main() when they are not passed
180      * to the target by the debugger.
181      */
182     char noname[2];
183     char *arg[2];
184     char *env[2];
185 
186 #if defined (__CRT_TRACE_INIT)
187 /*------------------*/
188 /* initialize trace */
189 /*------------------*/
190     {
191 	__ghs_trace_init();
192     }
193 #endif
194 
195 /*--------------------------*/
196 /* initialize board devices */
197 /*--------------------------*/
198     {
199 #if defined(__ghs_pic)
200 	static void (CONST_FUNCP board_devices_init_funcp)(void) =
201 	    __ghs_board_devices_init;
202 	if (board_devices_init_funcp)
203 #else  /* defined(__ghs_pic) */
204 	if (__ghs_board_devices_init)
205 #endif /* defined(__ghs_pic) */
206 	__ghs_board_devices_init();
207     }
208 
209 /*-----------------*/
210 /* initialize lock */
211 /*-----------------*/
212     {
213 #if defined(__ghs_pic)
214 	static void (CONST_FUNCP lock_init_funcp)(void) = __gh_lock_init;
215 	/* If ind_thrd.c is loaded, initialize the C library lock */
216 	if (lock_init_funcp)
217 #else  /* defined(__ghs_pic) */
218 	if (__gh_lock_init)
219 #endif /* defined(__ghs_pic) */
220 	 __gh_lock_init();
221     }
222 
223 /*----------------*/
224 /* initialize iob */
225 /*----------------*/
226     {
227 #if defined(__ghs_pic)
228 	static void (CONST_FUNCP iob_init_funcp)(void) = __gh_iob_init;
229 	/* if ind_iob.c is loaded, initialize _iob for stdin,stdout,stderr */
230 	if (iob_init_funcp)
231 #else  /* defined(__ghs_pic) */
232 	if (__gh_iob_init)
233 #endif /* defined(__ghs_pic) */
234 	 __gh_iob_init();
235     }
236 
237 #if !defined(__disable_thread_safe_extensions) && !defined(__OSE)
238 /*--------------------*/
239 /* C++ Thread-safe    */
240 /* Exception handling */
241 /*--------------------*/
242     {
243 #if defined(__ghs_pic)
244 	static void (CONST_FUNCP cpp_init_funcp)(void **) = __cpp_exception_init;
245 	if (cpp_init_funcp)
246 #else  /* defined(__ghs_pic) */
247 	if (__cpp_exception_init)
248 #endif /* defined(__ghs_pic) */
249 	    __ghs_cpp_exception_init();
250     }
251 #endif /* !defined(__disable_thread_safe_extensions) && !defined(__OSE) */
252 
253 /*-----------------------------*/
254 /* initialize manual profiling */
255 /*-----------------------------*/
256     {
257 #if defined(__ghs_pic)
258 	static void (CONST_FUNCP man_prf_funcp)(void) = __ghs_manprf_init;
259 	/* if ind_manprf.c is loaded, initialize manual profiling */
260 	if (man_prf_funcp)
261 #else  /* defined(__ghs_pic) */
262 	if (__ghs_manprf_init)
263 #endif /* defined(__ghs_pic) */
264 	__ghs_manprf_init();
265     }
266 
267 /*--------------------------------------------*/
268 /* set default arguments to main()            */
269 /*--------------------------------------------*/
270 
271     /* Check if the debugger did not supply arguments
272      * (for example, when executing out of flash)
273      */
274     if (!argc) {
275 	noname[0] = 0;
276 	noname[1] = 0;
277 
278 	arg[0] = noname;
279 	arg[1] = 0;
280 
281 	env[0] = noname+1;
282 	env[1] = 0;
283 
284 	envp = env;
285 	argv = arg;
286 
287 	argc = 1;
288     }
289     environ = envp;
290 
291 #if defined(__StarCore__)
292     /* for C++ with the SC3 compiler */
293     {
294 	/* make destructors be called when we exit */
295 	#pragma weak __destroy_global_chain
296 	static void (CONST_FUNCP dgc_funcp)(void) = __destroy_global_chain;
297 	if (dgc_funcp)
298 	    atexit(__destroy_global_chain);
299 
300 	/* call the static constructors */
301 	__exec_staticinit();
302     }
303 #endif
304 
305     /* This is a suitable location to place any additional initialization
306        routines that must execute prior to main()
307      */
308 
309 /*------------------------------*/
310 /* call main(argc, argv, envp)	*/
311 /*------------------------------*/
312     exit(main(argc, argv, envp));
313     /* exit() will shut down the C library and call _Exit() */
314     _Exit(-1);
315     /* _Exit() should never return. If it does, let our caller handle it.   */
316     return;
317 }
318 #endif // CAFE GHS ORIG
319 
320 #if defined(__StarCore__)
321 /* function needed for C++ with StarCore SC3 compiler
322  * this calls the static constructors */
323 #pragma weak __ghsbegin_staticinit
324 #pragma weak __ghsend_staticinit
325 extern void (*__ghsbegin_staticinit)(void);
326 extern void (*__ghsend_staticinit)(void);
327 
__exec_staticinit(void)328 void __exec_staticinit(void)
329 {
330     void (**cpp_staticinit)(void) = &__ghsbegin_staticinit;
331     void (*init_end)(void) = __ghsend_staticinit;
332 
333     while(*cpp_staticinit != init_end)
334     {
335 	(*cpp_staticinit++)();
336     }
337 }
338 #endif /* ___StarCore__ */
339