From 2310ddd47064d2945c9cb992231722046ec58ae4 Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Tue, 27 Aug 2024 17:58:39 +0200 Subject: [PATCH] init: declare _libc_init as constructor This allows constructor handling mechanism in dynamic linker. JIRA: RTOS-664 --- misc/init.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/misc/init.c b/misc/init.c index eba32b43..98a25456 100644 --- a/misc/init.c +++ b/misc/init.c @@ -24,8 +24,21 @@ extern void _init_array(void); extern void _pthread_init(void); -void _libc_init(void) +static int _libc_initialized; + + +/* _libc_init is called twice once explicitely in crt0 and once during constructor handling. + * If libc is linked statically first it is called explicitely and then during constructor handling. + * If libc is linked dynamically first it is called by the constructor handling mechanism in dynamic linker, + * to allow constructors from other objects to call libc functions. + */ +__attribute__((constructor)) void _libc_init(void) { + if (_libc_initialized != 0) { + return; + } + _libc_initialized = 1; + _atexit_init(); _errno_init(); _malloc_init();