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 Jan 27, 2024
1 parent 1079bc3 commit 86334a3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 84 deletions.
77 changes: 57 additions & 20 deletions crt0-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,84 @@
*
* Entrypoint
*
* Copyright 2017, 2018 Phoenix Systems
* Author: Pawel Pisarczyk, Aleksander Kaminski
* Copyright 2017, 2018, 2023 Phoenix Systems
* Author: Pawel Pisarczyk, Aleksander Kaminski, Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %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);


__attribute__((noinline)) static 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);


static 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);


char **environ;
const char *argv_progname;


void _startc(int argc, char **argv, char **env)
__attribute__((noreturn)) 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();

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

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.

44 changes: 19 additions & 25 deletions misc/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,34 @@
*
* libphoenix
*
* init array
* init
*
* Copyright 2021 Phoenix Systems
* Author: Hubert Buczynski
* Copyright 2021, 2023 Phoenix Systems
* Author: Hubert Buczynski, Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#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 86334a3

Please sign in to comment.