[libc] Pull crt0 out into its own module

Sorta. crt0 is a separate module as far as bonnibel is concerned, but it's
still part of the libc module file.
This commit is contained in:
Justin C. Miller
2024-02-13 22:41:40 -08:00
parent 75d30fb56d
commit ba6e8e1349
7 changed files with 28 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
#include <stddef.h>
using cb = void (*)(void);
extern cb __preinit_array_start;
extern cb __preinit_array_end;
extern cb __init_array_start;
extern cb __init_array_end;
namespace {
void
run_ctor_list(cb *array, cb *end)
{
if (!array || !end)
return;
size_t i = 0;
while (true) {
const cb &ctor = array[i++];
if (&ctor == end) return;
if (ctor) ctor();
}
}
void
run_global_ctors()
{
run_ctor_list(&__preinit_array_start, &__preinit_array_end);
run_ctor_list(&__init_array_start, &__init_array_end);
}
} // namespace
extern "C"
void __init_libc()
{
run_global_ctors();
}