diff options
author | Thomas Weißschuh <linux@weissschuh.net> | 2023-10-05 19:17:29 +0300 |
---|---|---|
committer | Thomas Weißschuh <linux@weissschuh.net> | 2023-10-12 22:14:16 +0300 |
commit | 63aa531716268f22f0a60fbb65c005494dcde387 (patch) | |
tree | ea27f9c9b7df0f3c361a291b9bd902ea7c499e43 /tools/include/nolibc/crt.h | |
parent | eddfc3c74214a7e6f4a3e56ad0cf5dab5d23f287 (diff) | |
download | linux-63aa531716268f22f0a60fbb65c005494dcde387.tar.xz |
tools/nolibc: add support for constructors and destructors
With the startup code moved to C, implementing support for
constructors and deconstructors is fairly easy to implement.
Examples for code size impact:
text data bss dec hex filename
21837 104 88 22029 560d nolibc-test.before
22135 120 88 22343 5747 nolibc-test.after
21970 104 88 22162 5692 nolibc-test.after-only-crt.h-changes
The sections are defined by [0].
[0] https://refspecs.linuxfoundation.org/elf/gabi4+/ch5.dynamic.html
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/lkml/20231007-nolibc-constructors-v2-1-ef84693efbc1@weissschuh.net/
Diffstat (limited to 'tools/include/nolibc/crt.h')
-rw-r--r-- | tools/include/nolibc/crt.h | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h index a05655b4ce1d..43b551468c2a 100644 --- a/tools/include/nolibc/crt.h +++ b/tools/include/nolibc/crt.h @@ -13,12 +13,23 @@ const unsigned long *_auxv __attribute__((weak)); static void __stack_chk_init(void); static void exit(int); +extern void (*const __preinit_array_start[])(void) __attribute__((weak)); +extern void (*const __preinit_array_end[])(void) __attribute__((weak)); + +extern void (*const __init_array_start[])(void) __attribute__((weak)); +extern void (*const __init_array_end[])(void) __attribute__((weak)); + +extern void (*const __fini_array_start[])(void) __attribute__((weak)); +extern void (*const __fini_array_end[])(void) __attribute__((weak)); + __attribute__((weak)) void _start_c(long *sp) { long argc; char **argv; char **envp; + int exitcode; + void (* const *func)(void); const unsigned long *auxv; /* silence potential warning: conflicting types for 'main' */ int _nolibc_main(int, char **, char **) __asm__ ("main"); @@ -55,8 +66,18 @@ void _start_c(long *sp) ; _auxv = auxv; + for (func = __preinit_array_start; func < __preinit_array_end; func++) + (*func)(); + for (func = __init_array_start; func < __init_array_end; func++) + (*func)(); + /* go to application */ - exit(_nolibc_main(argc, argv, envp)); + exitcode = _nolibc_main(argc, argv, envp); + + for (func = __fini_array_end; func > __fini_array_start;) + (*--func)(); + + exit(exitcode); } #endif /* _NOLIBC_CRT_H */ |