[libc] Run the .preinit_array as well in __init_libc

The __init_libc function was already running the .init_array functions,
but was never running the .preinit_array functions. Now it runs them
both, in the correct order.
This commit is contained in:
Justin C. Miller
2022-02-13 00:09:36 -08:00
parent 3be4b103a2
commit b46b6363ff

View File

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