Rev 3593 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3593 | Rev 3805 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * crt1.c |
2 | * crt1.c |
3 | * This file has no copyright assigned and is placed in the Public Domain. |
3 | * This file has no copyright assigned and is placed in the Public Domain. |
4 | * This file is a part of the mingw-runtime package. |
4 | * This file is a part of the mingw-runtime package. |
5 | * No warranty is given; refer to the file DISCLAIMER within the package. |
5 | * No warranty is given; refer to the file DISCLAIMER within the package. |
6 | * |
6 | * |
7 | * Source code for the startup proceedures used by all programs. This code |
7 | * Source code for the startup proceedures used by all programs. This code |
8 | * is compiled to make crt1.o, which should be located in the library path. |
8 | * is compiled to make crt1.o, which should be located in the library path. |
9 | * |
9 | * |
10 | */ |
10 | */ |
11 | 11 | ||
12 | /* Hide the declaration of _fmode with dllimport attribute in stdlib.h to |
12 | /* Hide the declaration of _fmode with dllimport attribute in stdlib.h to |
13 | avoid problems with older GCC. */ |
13 | avoid problems with older GCC. */ |
14 | 14 | ||
15 | #include |
15 | #include |
16 | #include |
16 | #include |
17 | #include |
17 | #include |
18 | #include |
18 | #include |
19 | #include |
19 | #include |
20 | #include |
20 | #include |
21 | 21 | ||
22 | #include "cpu_features.h" |
22 | #include "cpu_features.h" |
23 | 23 | ||
24 | 24 | ||
25 | /* NOTE: The code for initializing the _argv, _argc, and environ variables |
25 | /* NOTE: The code for initializing the _argv, _argc, and environ variables |
26 | * has been moved to a separate .c file which is included in both |
26 | * has been moved to a separate .c file which is included in both |
27 | * crt1.c and dllcrt1.c. This means changes in the code don't have to |
27 | * crt1.c and dllcrt1.c. This means changes in the code don't have to |
28 | * be manually synchronized, but it does lead to this not-generally- |
28 | * be manually synchronized, but it does lead to this not-generally- |
29 | * a-good-idea use of include. */ |
29 | * a-good-idea use of include. */ |
30 | 30 | ||
31 | 31 | ||
32 | extern char __cmdline; |
32 | extern char __cmdline[]; |
33 | extern char __pgmname; |
33 | extern char __pgmname[]; |
34 | 34 | ||
35 | extern int main (int, char **, char **); |
35 | extern int main (int, char **, char **); |
36 | 36 | ||
37 | int _errno; |
37 | int _errno; |
38 | int _fmode; |
38 | int _fmode; |
39 | 39 | ||
40 | char __appcwd[1024]; |
40 | char __appcwd[1024]; |
41 | int __appcwdlen; |
41 | int __appcwdlen; |
42 | 42 | ||
43 | int _argc; |
43 | int _argc; |
44 | char **_argv; |
44 | char **_argv; |
45 | 45 | ||
46 | static char *arg[2]; |
46 | static char *arg[2]; |
47 | 47 | ||
48 | void _exit(int __status) __attribute__((noreturn)); |
48 | void _exit(int __status) __attribute__((noreturn)); |
49 | 49 | ||
50 | 50 | ||
51 | char * __libc_getenv(const char *name) |
51 | char * __libc_getenv(const char *name) |
52 | { |
52 | { |
53 | return NULL; |
53 | return NULL; |
54 | } |
54 | } |
55 | 55 | ||
56 | void __main (){}; |
56 | void __main (){}; |
57 | 57 | ||
58 | struct app_hdr |
58 | struct app_hdr |
59 | { |
59 | { |
60 | char banner[8]; |
60 | char banner[8]; |
61 | int version; |
61 | int version; |
62 | int start; |
62 | int start; |
63 | int iend; |
63 | int iend; |
64 | int memsize; |
64 | int memsize; |
65 | int stacktop; |
65 | int stacktop; |
66 | char *cmdline; |
66 | char *cmdline; |
67 | char *path; |
67 | char *path; |
68 | }; |
68 | }; |
69 | 69 | ||
70 | typedef void (*ctp)(); |
70 | typedef void (*ctp)(); |
71 | static void __do_global_ctors () |
71 | static void __do_global_ctors () |
72 | { |
72 | { |
73 | extern int __CTOR_LIST__; |
73 | extern int __CTOR_LIST__; |
74 | int *c = &__CTOR_LIST__; |
74 | int *c = &__CTOR_LIST__; |
75 | c++; |
75 | c++; |
76 | while (*c) |
76 | while (*c) |
77 | { |
77 | { |
78 | ctp d = (ctp)*c; |
78 | ctp d = (ctp)*c; |
79 | (d)(); |
79 | (d)(); |
80 | c++; |
80 | c++; |
81 | } |
81 | } |
82 | } |
82 | } |
83 | 83 | ||
84 | void __attribute__((noreturn)) |
84 | void __attribute__((noreturn)) |
85 | __crt_startup (void) |
85 | __crt_startup (void) |
86 | { |
86 | { |
87 | int nRet; |
87 | int nRet; |
88 | struct app_hdr *header; |
88 | struct app_hdr *header; |
89 | 89 | ||
90 | 90 | ||
91 | init_global_reent(); |
91 | init_global_reent(); |
92 | 92 | ||
93 | /* |
93 | /* |
94 | * Initialize floating point unit. |
94 | * Initialize floating point unit. |
95 | */ |
95 | */ |
96 | __cpu_features_init (); /* Do we have SSE, etc.*/ |
96 | __cpu_features_init (); /* Do we have SSE, etc.*/ |
97 | // _fpreset (); /* Supplied by the runtime library. */ |
97 | // _fpreset (); /* Supplied by the runtime library. */ |
98 | 98 | ||
99 | __do_global_ctors(); |
99 | __do_global_ctors(); |
100 | 100 | ||
101 | __appcwdlen = strrchr(&__pgmname, '/') - &__pgmname + 1; |
101 | __appcwdlen = strrchr(&__pgmname, '/') - &__pgmname + 1; |
102 | __appcwdlen = __appcwdlen > 1023 ? 1023 : __appcwdlen; |
102 | __appcwdlen = __appcwdlen > 1023 ? 1023 : __appcwdlen; |
103 | memcpy(__appcwd, &__pgmname, __appcwdlen); |
103 | memcpy(__appcwd, &__pgmname, __appcwdlen); |
104 | __appcwd[__appcwdlen] = 0; |
104 | __appcwd[__appcwdlen] = 0; |
105 | 105 | ||
106 | set_cwd(__appcwd); |
106 | set_cwd(__appcwd); |
107 | 107 | ||
108 | arg[0] = &__pgmname; |
108 | arg[0] = &__pgmname; |
109 | 109 | ||
110 | if( __cmdline != 0) |
110 | if( __cmdline[0] != 0) |
111 | { |
111 | { |
112 | _argc = 2; |
112 | _argc = 2; |
113 | arg[1] = &__cmdline; |
113 | arg[1] = &__cmdline; |
114 | } else _argc = 1; |
114 | } else _argc = 1; |
115 | 115 | ||
116 | _argv = arg; |
116 | _argv = arg; |
117 | 117 | ||
118 | /* |
118 | /* |
119 | * Sets the default file mode. |
119 | * Sets the default file mode. |
120 | * If _CRT_fmode is set, also set mode for stdin, stdout |
120 | * If _CRT_fmode is set, also set mode for stdin, stdout |
121 | * and stderr, as well |
121 | * and stderr, as well |
122 | * NOTE: DLLs don't do this because that would be rude! |
122 | * NOTE: DLLs don't do this because that would be rude! |
123 | */ |
123 | */ |
124 | // _mingw32_init_fmode (); |
124 | // _mingw32_init_fmode (); |
125 | 125 | ||
126 | 126 | ||
127 | nRet = main (_argc, _argv, NULL); |
127 | nRet = main (_argc, _argv, NULL); |
128 | 128 | ||
129 | /* |
129 | /* |
130 | * Perform exit processing for the C library. This means |
130 | * Perform exit processing for the C library. This means |
131 | * flushing output and calling 'atexit' registered functions. |
131 | * flushing output and calling 'atexit' registered functions. |
132 | */ |
132 | */ |
133 | exit (nRet); |
133 | exit (nRet); |
134 | } |
134 | } |