Skip to content

Commit

Permalink
Change libphoenix initialization
Browse files Browse the repository at this point in the history
This change prepares libphoenix to be used in dynamic loader, where crt0
is not included.

Moved constructor and destrutor calling into crt0.
Wrapped libphoenix initialization into a single function to reduce crt0
and libphoenix intertwine.

JIRA: RTOS-694
  • Loading branch information
badochov committed Nov 29, 2023
1 parent a8ac6ed commit bdcf9c3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 79 deletions.
71 changes: 54 additions & 17 deletions _startc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,60 @@
* %LICENSE%
*/

#include <sys/file.h>
#include <stdlib.h>
#include <stdio.h>


extern void _malloc_init(void);
extern int _env_init(void);
extern void _signals_init(void);
extern void _file_init(void);
extern void _errno_init(void);
extern void _atexit_init(void);
extern void _init_array(void);
extern void _pthread_init(void);
extern void (*__preinit_array_start[])(void);
extern void (*__preinit_array_end[])(void);

extern void (*__init_array_start[])(void);
extern void (*__init_array_end[])(void);


extern void _init(void);


void _init_array(void)
{
size_t i, sz;

sz = __preinit_array_end - __preinit_array_start;
for (i = 0; i < sz; i++) {
__preinit_array_start[i]();
}

/* FIXME: change compilation settings to make access to _init() */
/* _init(); */

sz = __init_array_end - __init_array_start;
for (i = 0; i < sz; i++) {
__init_array_start[i]();
}
}


extern void (*__fini_array_start[])(void);
extern void (*__fini_array_end[])(void);


extern void _fini(void);


void _fini_array(void)
{
size_t i, sz;

sz = __fini_array_end - __fini_array_start;
for (i = sz; i > 0; i--) {
__fini_array_start[i - 1]();
}

/* FIXME: change compilation settings to make access to _fini() */
/* _fini(); */
}


extern void _libc_init(void);
extern int main(int argc, char **argv);


Expand All @@ -38,14 +79,10 @@ void _startc(int argc, char **argv, char **env)
environ = env;
argv_progname = *argv;

_atexit_init();
_errno_init();
_malloc_init();
_env_init();
_signals_init();
_file_init();
_libc_init();

_init_array();
_pthread_init();
atexit(_fini_array);

exit(main(argc, argv));
}
2 changes: 1 addition & 1 deletion misc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# Copyright 2021 Phoenix Systems
#

OBJS += $(addprefix $(PREFIX_O)misc/, init.o fini.o)
OBJS += $(addprefix $(PREFIX_O)misc/, init.o)
36 changes: 0 additions & 36 deletions misc/fini.c

This file was deleted.

40 changes: 17 additions & 23 deletions misc/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* libphoenix
*
* init array
* init
*
* Copyright 2021 Phoenix Systems
* Author: Hubert Buczynski
Expand All @@ -14,29 +14,23 @@
*/


#include <stdlib.h>
extern void _malloc_init(void);
extern int _env_init(void);
extern void _signals_init(void);
extern void _file_init(void);
extern void _errno_init(void);
extern void _atexit_init(void);
extern void _init_array(void);
extern void _pthread_init(void);

extern void (*__preinit_array_start[])(void);
extern void (*__preinit_array_end[])(void);

extern void (*__init_array_start[])(void);
extern void (*__init_array_end[])(void);


extern void _init (void);

void _init_array(void)
void _libc_init(void)
{
size_t i, sz;

sz = __preinit_array_end - __preinit_array_start;
for (i = 0; i < sz; i++)
__preinit_array_start[i]();

/* FIXME: change compilation settings to make access to _init() */
/* _init(); */

sz = __init_array_end - __init_array_start;
for (i = 0; i < sz; i++)
__init_array_start[i]();
_atexit_init();
_errno_init();
_malloc_init();
_env_init();
_signals_init();
_file_init();
_pthread_init();
}
2 changes: 0 additions & 2 deletions stdlib/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <stdio.h>

extern void _atexit_call(void);
extern void _fini_array(void);
extern void sys_exit(int) __attribute__((noreturn));


Expand All @@ -37,7 +36,6 @@ void exit(int status)
{
fflush(NULL);
_atexit_call();
_fini_array();
_exit(status);
for(;;);
}

0 comments on commit bdcf9c3

Please sign in to comment.