mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[ld.so] Call all image global ctors, not just libc
With the move to dynamic executables, crt0's _start was only ever calling libc's __init_libc, which only ran libc's init_array list. Now make crt0 itself (which is statically linked into every executable) call it's own init_array list and have ld.so call every other image's ctor lists.
This commit is contained in:
@@ -5,6 +5,52 @@ extern exit
|
||||
extern __init_libj6
|
||||
extern __init_libc
|
||||
|
||||
extern __preinit_array_start
|
||||
extern __preinit_array_end
|
||||
extern __init_array_start
|
||||
extern __init_array_end
|
||||
|
||||
global __run_ctor_list:function hidden (__run_ctor_list.end - __run_ctor_list)
|
||||
__run_ctor_list:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
|
||||
push rbx
|
||||
push r12
|
||||
mov rbx, rdi
|
||||
mov r12, rsi
|
||||
.start:
|
||||
cmp rbx, r12
|
||||
je .fin
|
||||
mov rax, [rbx]
|
||||
call rax
|
||||
add rbx, 8
|
||||
jmp .start
|
||||
.fin:
|
||||
|
||||
pop r12
|
||||
pop rbx
|
||||
pop rbp
|
||||
ret
|
||||
.end:
|
||||
|
||||
global __run_global_ctors:function hidden (__run_global_ctors.end - __run_global_ctors)
|
||||
__run_global_ctors:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
|
||||
lea rdi, [rel __preinit_array_start]
|
||||
lea rsi, [rel __preinit_array_end]
|
||||
call __run_ctor_list
|
||||
lea rdi, [rel __init_array_start]
|
||||
lea rsi, [rel __init_array_end]
|
||||
call __run_ctor_list
|
||||
|
||||
pop rbp
|
||||
ret
|
||||
.end:
|
||||
|
||||
|
||||
; Put the address of the given symbol in rax
|
||||
; This macro is the same as in util/got.inc,
|
||||
; but crt0 can't have a dep on libutil
|
||||
@@ -31,6 +77,8 @@ _libc_crt0_start:
|
||||
lookup_GOT __init_libc
|
||||
call rax
|
||||
|
||||
call __run_global_ctors
|
||||
|
||||
mov rdi, 0
|
||||
mov rsi, rsp
|
||||
mov rdx, 0 ; TODO: actually parse stack for argc, argv, envp
|
||||
|
||||
@@ -1,33 +1,6 @@
|
||||
#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 *p, cb *end)
|
||||
{
|
||||
while (p && end && p < end) {
|
||||
if (p) (*p)();
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user