Skip to content

Commit 63aa531

Browse files
committed
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 <[email protected]> Acked-by: Willy Tarreau <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/
1 parent eddfc3c commit 63aa531

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

tools/include/nolibc/crt.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@ const unsigned long *_auxv __attribute__((weak));
1313
static void __stack_chk_init(void);
1414
static void exit(int);
1515

16+
extern void (*const __preinit_array_start[])(void) __attribute__((weak));
17+
extern void (*const __preinit_array_end[])(void) __attribute__((weak));
18+
19+
extern void (*const __init_array_start[])(void) __attribute__((weak));
20+
extern void (*const __init_array_end[])(void) __attribute__((weak));
21+
22+
extern void (*const __fini_array_start[])(void) __attribute__((weak));
23+
extern void (*const __fini_array_end[])(void) __attribute__((weak));
24+
1625
__attribute__((weak))
1726
void _start_c(long *sp)
1827
{
1928
long argc;
2029
char **argv;
2130
char **envp;
31+
int exitcode;
32+
void (* const *func)(void);
2233
const unsigned long *auxv;
2334
/* silence potential warning: conflicting types for 'main' */
2435
int _nolibc_main(int, char **, char **) __asm__ ("main");
@@ -55,8 +66,18 @@ void _start_c(long *sp)
5566
;
5667
_auxv = auxv;
5768

69+
for (func = __preinit_array_start; func < __preinit_array_end; func++)
70+
(*func)();
71+
for (func = __init_array_start; func < __init_array_end; func++)
72+
(*func)();
73+
5874
/* go to application */
59-
exit(_nolibc_main(argc, argv, envp));
75+
exitcode = _nolibc_main(argc, argv, envp);
76+
77+
for (func = __fini_array_end; func > __fini_array_start;)
78+
(*--func)();
79+
80+
exit(exitcode);
6081
}
6182

6283
#endif /* _NOLIBC_CRT_H */

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static int test_argc;
5757
/* will be used by some test cases as readable file, please don't write it */
5858
static const char *argv0;
5959

60+
/* will be used by constructor tests */
61+
static int constructor_test_value;
62+
6063
/* definition of a series of tests */
6164
struct test {
6265
const char *name; /* test name */
@@ -594,6 +597,19 @@ int expect_strne(const char *expr, int llen, const char *cmp)
594597
#define CASE_TEST(name) \
595598
case __LINE__: llen += printf("%d %s", test, #name);
596599

600+
/* constructors validate that they are executed in definition order */
601+
__attribute__((constructor))
602+
static void constructor1(void)
603+
{
604+
constructor_test_value = 1;
605+
}
606+
607+
__attribute__((constructor))
608+
static void constructor2(void)
609+
{
610+
constructor_test_value *= 2;
611+
}
612+
597613
int run_startup(int min, int max)
598614
{
599615
int test;
@@ -630,6 +646,7 @@ int run_startup(int min, int max)
630646
CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break;
631647
CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
632648
CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
649+
CASE_TEST(constructor); EXPECT_EQ(1, constructor_test_value, 2); break;
633650
case __LINE__:
634651
return ret; /* must be last */
635652
/* note: do not set any defaults so as to permit holes above */

0 commit comments

Comments
 (0)