1 | /* |
---|
2 | * rtems-ctor.cc |
---|
3 | * |
---|
4 | * Description: |
---|
5 | * This file exists solely to (try to) ensure RTEMS is initialized |
---|
6 | * before any global constructors are run. |
---|
7 | * |
---|
8 | * The problem: |
---|
9 | * Global constructors might reasonably expect that new() will |
---|
10 | * work, but since new() uses malloc() which uses RTEMS regions, |
---|
11 | * it can not be called until after initialize_executive(). |
---|
12 | * |
---|
13 | * Global constructors are called in GNU systems one of 2 ways: |
---|
14 | * |
---|
15 | * an "invisible" call to __main() inserted by compiler |
---|
16 | * This __main() calls __do_global_ctors() which |
---|
17 | * walks thru the table and calls all global |
---|
18 | * constructors. |
---|
19 | * |
---|
20 | * or - |
---|
21 | * A special section is put into the linked binary. The |
---|
22 | * system startup code knows to run the constructors in |
---|
23 | * this special section before calling main(). |
---|
24 | * |
---|
25 | * By making RTEMS initialization a constructor, we avoid having |
---|
26 | * too much about all this. All we have to guarantee is that |
---|
27 | * this constructor is the first one run. |
---|
28 | * |
---|
29 | * |
---|
30 | * So for the first case above, this is what happens |
---|
31 | * |
---|
32 | * host crt0 |
---|
33 | * main() |
---|
34 | * __main() |
---|
35 | * __do_global_ctors() |
---|
36 | * bsp_start() |
---|
37 | * init_executive_early() |
---|
38 | * <<any other constructors>> |
---|
39 | * |
---|
40 | * init_executive_late() |
---|
41 | * bsp_cleanup() |
---|
42 | * |
---|
43 | * TODO: |
---|
44 | * |
---|
45 | * $Id$ |
---|
46 | */ |
---|
47 | |
---|
48 | #include <bsp.h> |
---|
49 | #include <stdio.h> |
---|
50 | #include <stdlib.h> |
---|
51 | |
---|
52 | /* |
---|
53 | * RTEMS program name |
---|
54 | * Probably not used by anyone, but it is nice to have it. |
---|
55 | * Actually the UNIX version of CPU_INVOKE_DEBUGGER will probably |
---|
56 | * need to use it |
---|
57 | */ |
---|
58 | |
---|
59 | char *rtems_progname; |
---|
60 | |
---|
61 | class RTEMS { |
---|
62 | public: |
---|
63 | RTEMS(); |
---|
64 | ~RTEMS(); |
---|
65 | }; |
---|
66 | |
---|
67 | RTEMS rtems_constructor; |
---|
68 | |
---|
69 | RTEMS::RTEMS() |
---|
70 | { |
---|
71 | bsp_start(); |
---|
72 | } |
---|
73 | |
---|
74 | RTEMS::~RTEMS() |
---|
75 | { |
---|
76 | bsp_cleanup(); |
---|
77 | } |
---|
78 | |
---|
79 | extern "C" { |
---|
80 | extern void invoke_non_gnu_constructors(void); |
---|
81 | |
---|
82 | int |
---|
83 | main(int argc, |
---|
84 | char **argv, |
---|
85 | char **environp) |
---|
86 | { |
---|
87 | rtems_argc = argc; |
---|
88 | rtems_argv = argv; |
---|
89 | |
---|
90 | if ((argc > 0) && argv && argv[0]) |
---|
91 | rtems_progname = argv[0]; |
---|
92 | else |
---|
93 | rtems_progname = "RTEMS"; |
---|
94 | |
---|
95 | /* |
---|
96 | * run any non-gnu constructors we may need |
---|
97 | */ |
---|
98 | |
---|
99 | invoke_non_gnu_constructors(); |
---|
100 | |
---|
101 | /* |
---|
102 | * Start multitasking |
---|
103 | */ |
---|
104 | |
---|
105 | rtems_initialize_executive_late( bsp_isr_level ); |
---|
106 | |
---|
107 | /* |
---|
108 | * Returns when multitasking is stopped |
---|
109 | * This allows our destructors to get run normally |
---|
110 | */ |
---|
111 | |
---|
112 | fflush( stdout ); |
---|
113 | fflush( stderr ); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | } |
---|