elf/startup: Add __mlibc_run_constructors() function for static linking

This commit is contained in:
mintsuki 2019-09-28 11:54:38 +02:00
parent b57ce631aa
commit 02e740c90d
2 changed files with 26 additions and 0 deletions

View file

@ -6,6 +6,30 @@
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>
extern "C" size_t __init_array_start[];
extern "C" size_t __init_array_end[];
static int constructors_ran_already = 0;
struct global_constructor_guard {
global_constructor_guard() {
constructors_ran_already = 1;
}
};
static global_constructor_guard g;
void __mlibc_run_constructors() {
if (!constructors_ran_already) {
size_t constructor_count = (size_t)__init_array_end - (size_t)__init_array_start;
constructor_count /= sizeof(void*);
for (size_t i = 0; i < constructor_count; i++) {
void (*ptr)(void) = (void(*)(void))(__init_array_start[i]);
ptr();
}
}
}
namespace mlibc {
void parse_exec_stack(void *opaque_sp, exec_stack_data *data) {

View file

@ -1,6 +1,8 @@
#ifndef MLIBC_ELF_STARTUP
#define MLIBC_ELF_STARTUP
void __mlibc_run_constructors();
namespace mlibc {
struct exec_stack_data {