[libc] Call global ctors in user code
This change adds a new __init_libc function which calls all the global ctors in .init_array, and is called from _start.
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
extern main
|
||||
extern exit
|
||||
extern __init_libj6
|
||||
extern __init_libc
|
||||
|
||||
global _start:function (_start.end - _start)
|
||||
_start:
|
||||
mov rbp, rsp
|
||||
mov rdi, rsp
|
||||
call __init_libj6
|
||||
call __init_libc
|
||||
|
||||
pop rdi
|
||||
mov rsi, rsp
|
||||
|
||||
27
src/libraries/libc/arch/amd64/crt/init.cpp
Normal file
27
src/libraries/libc/arch/amd64/crt/init.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stddef.h>
|
||||
|
||||
using cb = void (*)(void);
|
||||
extern cb __init_array_start[0];
|
||||
extern cb __init_array_end;
|
||||
|
||||
namespace {
|
||||
|
||||
void
|
||||
run_global_ctors()
|
||||
{
|
||||
size_t i = 0;
|
||||
while (true) {
|
||||
const cb &fp = __init_array_start[i++];
|
||||
if (&fp == &__init_array_end)
|
||||
return;
|
||||
fp();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C"
|
||||
void __init_libc()
|
||||
{
|
||||
run_global_ctors();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
extern main
|
||||
extern exit
|
||||
extern __init_libj6
|
||||
extern __init_libc
|
||||
extern _arg_modules_phys
|
||||
|
||||
section .bss
|
||||
@@ -28,6 +29,7 @@ _start:
|
||||
mov rbp, rsp
|
||||
mov rdi, rsp
|
||||
call __init_libj6
|
||||
call __init_libc
|
||||
|
||||
pop rdi
|
||||
mov rsi, rsp
|
||||
|
||||
Reference in New Issue
Block a user