From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/tests/rtdl/preinit/libfoo.c | 18 +++++++++++++ lib/mlibc/tests/rtdl/preinit/meson.build | 18 +++++++++++++ lib/mlibc/tests/rtdl/preinit/test.c | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 lib/mlibc/tests/rtdl/preinit/libfoo.c create mode 100644 lib/mlibc/tests/rtdl/preinit/meson.build create mode 100644 lib/mlibc/tests/rtdl/preinit/test.c (limited to 'lib/mlibc/tests/rtdl/preinit') diff --git a/lib/mlibc/tests/rtdl/preinit/libfoo.c b/lib/mlibc/tests/rtdl/preinit/libfoo.c new file mode 100644 index 0000000..9c834ea --- /dev/null +++ b/lib/mlibc/tests/rtdl/preinit/libfoo.c @@ -0,0 +1,18 @@ +#include +#include + +int fooDone = 0; + +// DSOs do not support pre-initialization functions. + +__attribute__((constructor)) +void fooInit() { + dprintf(1, "initialization function called in foo\n"); + + assert(fooDone == 0); + fooDone++; +} + +int isFooDone() { + return fooDone; +} diff --git a/lib/mlibc/tests/rtdl/preinit/meson.build b/lib/mlibc/tests/rtdl/preinit/meson.build new file mode 100644 index 0000000..1a7f398 --- /dev/null +++ b/lib/mlibc/tests/rtdl/preinit/meson.build @@ -0,0 +1,18 @@ +if host_machine.cpu_family() == 'riscv64' + # gp isn't initialized until after crt1.o runs, so to access + # globals in our pre-initializers we must disable it. + test_additional_link_args = ['-Wl,--no-relax'] +endif + +libfoo = shared_library('foo', 'libfoo.c', + dependencies: libc_dep, + override_options: 'b_sanitize=none', + link_args: test_additional_link_args, +) +test_link_with = [libfoo] + +libfoo_native = shared_library('native-foo', 'libfoo.c', + link_args: ['-ldl'] + test_additional_link_args, + native: true +) +test_native_link_with = [libfoo_native] diff --git a/lib/mlibc/tests/rtdl/preinit/test.c b/lib/mlibc/tests/rtdl/preinit/test.c new file mode 100644 index 0000000..5b5d5e8 --- /dev/null +++ b/lib/mlibc/tests/rtdl/preinit/test.c @@ -0,0 +1,44 @@ +#include +#include + +int mainDone = 0; + +int isFooDone(); + +void preInit1() { + // Use dprintf because stdout might not be initialized yet. + dprintf(1, "pre-initialization function 1 called in main executable\n"); + + assert(isFooDone() == 0); + assert(mainDone == 0); + mainDone++; +} + +void preInit2() { + dprintf(1, "pre-initialization function 2 called in main executable\n"); + + assert(isFooDone() == 0); + assert(mainDone == 1); + mainDone++; +} + +__attribute__((constructor)) +void mainInit() { + dprintf(1, "initialization function called in main executable\n"); + + assert(isFooDone() == 1); + assert(mainDone == 2); + mainDone++; +} + +// Manually register the pre-initialization functions. +__attribute__((used, section(".preinit_array"))) +static void (*preinitFunctions[])(void) = { + &preInit1, + &preInit2, +}; + +int main() { + assert(isFooDone() == 1); + assert(mainDone == 3); +} -- cgit v1.2.3