Skip to content

Commit b61299e

Browse files
committed
Move handling of .init_array to separate file
This allows applications to override __main() as before, when that code is not needed
1 parent 4440b47 commit b61299e

File tree

5 files changed

+73
-60
lines changed

5 files changed

+73
-60
lines changed

configvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ LDFLAGS=
143143
DEFS=
144144

145145
# Define this to the warning level you want.
146-
WARN=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -Wold-style-declaration -Wold-style-definition
146+
WARN=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -Wold-style-declaration -Wold-style-definition -Wdeclaration-after-statement
147147

148148
# If your system lacks a BSD compatible install you should set this
149149
# variable to "$(top_srcdir}/install-sh".

mintlib/SRCFILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ SRCFILES = \
113113
wctype.c \
114114
wcwidth.c \
115115
wcscoll.c \
116+
__main.c \
116117
\
117118
nf_debug.c \
118119
nf_name.c \

mintlib/__main.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdlib.h>
2+
#include "lib.h"
3+
4+
#ifdef __GCC_HAVE_INITFINI_ARRAY_SUPPORT
5+
6+
/*
7+
* Support for the init/fini initialization mechanism.
8+
* Inspired from glibc's csu/libc-start.c.
9+
* https://sourceware.org/git/?p=glibc.git;f=csu/libc-start.c;hb=refs/heads/master
10+
*/
11+
12+
/* These magic symbols are provided by the linker. */
13+
extern void (*__preinit_array_start [])(int, char **, char **);
14+
extern void (*__preinit_array_end [])(int, char **, char **);
15+
extern void (*__init_array_start [])(int, char **, char **);
16+
extern void (*__init_array_end [])(int, char **, char **);
17+
extern void (*__fini_array_start [])(void);
18+
extern void (*__fini_array_end [])(void);
19+
20+
/* Call global initializers/constructors. */
21+
static void
22+
call_init(int argc, char **argv, char **envp)
23+
{
24+
size_t size;
25+
size_t i;
26+
27+
/* Preinitializers come from the .preinit_array sections. */
28+
size = __preinit_array_end - __preinit_array_start;
29+
for (i = 0; i < size; i++)
30+
(*__preinit_array_start [i])(argc, argv, envp);
31+
32+
/* Modern systems don't require the .init section. */
33+
/*_init();*/
34+
35+
/* Initializers come from the .init_array sections. */
36+
size = __init_array_end - __init_array_start;
37+
for (i = 0; i < size; i++)
38+
(*__init_array_start [i])(argc, argv, envp);
39+
}
40+
41+
/* Call global finalizers/destructors. */
42+
static void
43+
call_fini(void)
44+
{
45+
/* Finalizers come from the .fini_array sections. */
46+
size_t i = __fini_array_end - __fini_array_start;
47+
while (i-- > 0)
48+
(*__fini_array_start [i])();
49+
50+
/* Modern systems don't require the .fini section. */
51+
/*_fini();*/
52+
}
53+
54+
void __main(void)
55+
{
56+
atexit(call_fini);
57+
call_init(__libc_argc, __libc_argv, environ);
58+
}
59+
60+
#endif /* __GCC_HAVE_INITFINI_ARRAY_SUPPORT */

mintlib/crtinit.c

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -86,58 +86,6 @@ char **__libc_argv = __libc_argv_default;
8686

8787
static long parseargs(BASEPAGE *);
8888

89-
#ifdef __ELF__
90-
91-
/*
92-
* Support for the init/fini initialization mechanism.
93-
* Inspired from glibc's csu/libc-start.c.
94-
* https://sourceware.org/git/?p=glibc.git;f=csu/libc-start.c;hb=refs/heads/master
95-
*/
96-
97-
/* These magic symbols are provided by the linker. */
98-
extern void (*__preinit_array_start [])(int, char **, char **);
99-
extern void (*__preinit_array_end [])(int, char **, char **);
100-
extern void (*__init_array_start [])(int, char **, char **);
101-
extern void (*__init_array_end [])(int, char **, char **);
102-
extern void (*__fini_array_start [])(void);
103-
extern void (*__fini_array_end [])(void);
104-
105-
/* Call global initializers/constructors. */
106-
static void
107-
call_init(int argc, char **argv, char **envp)
108-
{
109-
/* Preinitializers come from the .preinit_array sections. */
110-
{
111-
const size_t size = __preinit_array_end - __preinit_array_start;
112-
size_t i;
113-
for (i = 0; i < size; i++)
114-
(*__preinit_array_start [i])(argc, argv, envp);
115-
}
116-
117-
/* Modern systems don't require the .init section. */
118-
/*_init();*/
119-
120-
/* Initializers come from the .init_array sections. */
121-
const size_t size = __init_array_end - __init_array_start;
122-
for (size_t i = 0; i < size; i++)
123-
(*__init_array_start [i])(argc, argv, envp);
124-
}
125-
126-
/* Call global finalizers/destructors. */
127-
static void
128-
call_fini(void)
129-
{
130-
/* Finalizers come from the .fini_array sections. */
131-
size_t i = __fini_array_end - __fini_array_start;
132-
while (i-- > 0)
133-
(*__fini_array_start [i])();
134-
135-
/* Modern systems don't require the .fini section. */
136-
/*_fini();*/
137-
}
138-
139-
#endif /* __ELF__ */
140-
14189
/*
14290
* accessories start here:
14391
*/
@@ -166,13 +114,15 @@ _acc_main(void)
166114
/* this is an accessory */
167115
_app = 0;
168116

169-
#ifdef __ELF__
117+
__libc_argv = acc_argv;
118+
environ = acc_argv;
119+
120+
#ifdef __GCC_HAVE_INITFINI_ARRAY_SUPPORT
170121
/* main() won't call __main() for global constructors, so do it here. */
171-
atexit(call_fini);
172-
call_init(1L, acc_argv, acc_argv);
122+
__main();
173123
#endif
174124

175-
_main(1L, acc_argv, acc_argv);
125+
_main(__libc_argc, __libc_argv, environ);
176126
/*NOTREACHED*/
177127
}
178128

@@ -254,10 +204,9 @@ _crtinit(void)
254204
/* start profiling, if we were linked with gcrt0.o */
255205
_monstartup((void *)bp->p_tbase, (void *)((long)etext - 1));
256206

257-
#ifdef __ELF__
207+
#ifdef __GCC_HAVE_INITFINI_ARRAY_SUPPORT
258208
/* main() won't call __main() for global constructors, so do it here. */
259-
atexit(call_fini);
260-
call_init(__libc_argc, __libc_argv, environ);
209+
__main();
261210
#endif
262211

263212
_main(__libc_argc, __libc_argv, environ);

mintlib/lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void _main (long, char **, char **);
3939
void _acc_main (void);
4040
void _setstack (char *);
4141

42+
/* automagically called, either on entry to main(), or by use of .init_array */
43+
void __main (void);
44+
4245
void _monstartup (void *lowpc, void *highpc);
4346

4447

0 commit comments

Comments
 (0)