Kernel/LibC: Add support for init_array and fini_array
This commit is contained in:
@@ -21,6 +21,14 @@ _start:
|
||||
# call global constructors
|
||||
call _init
|
||||
|
||||
movl $__init_array_start, %ebx
|
||||
jmp 2f
|
||||
1: movl (%ebx), %eax
|
||||
call *%eax
|
||||
addl $4, %ebx
|
||||
2: cmpl $__init_array_end, %ebx
|
||||
jne 1b
|
||||
|
||||
# call main
|
||||
movl 0(%esp), %eax
|
||||
xchgl %eax, 8(%esp)
|
||||
|
||||
@@ -14,3 +14,11 @@ _fini:
|
||||
pushl %ebp
|
||||
movl %esp, %ebp
|
||||
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */
|
||||
|
||||
.section .init_array
|
||||
.global __init_array_start
|
||||
__init_array_start:
|
||||
|
||||
.section .fini_array
|
||||
.global __fini_array_start
|
||||
__fini_array_start:
|
||||
|
||||
@@ -8,3 +8,11 @@
|
||||
/* gcc will nicely put the contents of crtend.o's .fini section here. */
|
||||
popl %ebp
|
||||
ret
|
||||
|
||||
.section .init_array
|
||||
.global __init_array_end
|
||||
__init_array_end:
|
||||
|
||||
.section .fini_array
|
||||
.global __fini_array_end
|
||||
__fini_array_end:
|
||||
|
||||
@@ -22,6 +22,14 @@ _start:
|
||||
# call global constructors
|
||||
call _init
|
||||
|
||||
movq $__init_array_start, %rbx
|
||||
jmp 2f
|
||||
1: movq (%rbx), %rax
|
||||
call *%rax
|
||||
addq $8, %rbx
|
||||
2: cmpq $__init_array_end, %rbx
|
||||
jne 1b
|
||||
|
||||
# call main
|
||||
movq 16(%rsp), %rdi
|
||||
movq 8(%rsp), %rsi
|
||||
|
||||
@@ -14,3 +14,11 @@ _fini:
|
||||
pushq %rbp
|
||||
movq %rsp, %rbp
|
||||
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */
|
||||
|
||||
.section .init_array
|
||||
.global __init_array_start
|
||||
__init_array_start:
|
||||
|
||||
.section .fini_array
|
||||
.global __fini_array_start
|
||||
__fini_array_start:
|
||||
|
||||
@@ -8,3 +8,11 @@
|
||||
/* gcc will nicely put the contents of crtend.o's .fini section here. */
|
||||
popq %rbp
|
||||
ret
|
||||
|
||||
.section .init_array
|
||||
.global __init_array_end
|
||||
__init_array_end:
|
||||
|
||||
.section .fini_array
|
||||
.global __fini_array_end
|
||||
__fini_array_end:
|
||||
|
||||
@@ -48,7 +48,7 @@ struct malloc_pool_t
|
||||
|
||||
static malloc_pool_t s_malloc_pools[s_malloc_pool_count];
|
||||
|
||||
void init_malloc()
|
||||
void _init_malloc()
|
||||
{
|
||||
size_t pool_size = s_malloc_pool_size_initial;
|
||||
for (size_t i = 0; i < s_malloc_pool_count; i++)
|
||||
|
||||
@@ -15,20 +15,20 @@
|
||||
|
||||
struct FILE
|
||||
{
|
||||
int fd { -1 };
|
||||
mode_t mode { 0 };
|
||||
int buffer_type { _IOLBF };
|
||||
bool eof { false };
|
||||
bool error { false };
|
||||
int fd;
|
||||
mode_t mode;
|
||||
int buffer_type;
|
||||
bool eof;
|
||||
bool error;
|
||||
|
||||
int pid { -1 };
|
||||
int pid;
|
||||
|
||||
int unget_char { EOF };
|
||||
int unget_char;
|
||||
|
||||
unsigned char inline_buffer_storage[BUFSIZ] {};
|
||||
unsigned char* buffer = inline_buffer_storage;
|
||||
uint32_t buffer_size = BUFSIZ;
|
||||
uint32_t buffer_index { 0 };
|
||||
unsigned char inline_buffer_storage[BUFSIZ];
|
||||
unsigned char* buffer;
|
||||
uint32_t buffer_size;
|
||||
uint32_t buffer_index;
|
||||
};
|
||||
|
||||
struct ScopeLock
|
||||
@@ -47,18 +47,46 @@ struct ScopeLock
|
||||
FILE* m_file;
|
||||
};
|
||||
|
||||
static FILE s_files[FOPEN_MAX] {
|
||||
{ .fd = STDIN_FILENO, .mode = O_RDONLY },
|
||||
{ .fd = STDOUT_FILENO, .mode = O_WRONLY },
|
||||
{ .fd = STDERR_FILENO, .mode = O_WRONLY, .buffer_type = _IONBF },
|
||||
{ .fd = STDDBG_FILENO, .mode = O_WRONLY },
|
||||
};
|
||||
static FILE s_files[FOPEN_MAX];
|
||||
|
||||
FILE* stdin = &s_files[0];
|
||||
FILE* stdout = &s_files[1];
|
||||
FILE* stderr = &s_files[2];
|
||||
FILE* stddbg = &s_files[3];
|
||||
|
||||
static void init_closed_file(FILE* file)
|
||||
{
|
||||
file->fd = -1;
|
||||
file->mode = 0;
|
||||
file->buffer_type = _IOLBF;
|
||||
file->eof = false;
|
||||
file->error = false;
|
||||
file->pid = -1;
|
||||
file->unget_char = EOF;
|
||||
file->buffer = file->inline_buffer_storage;
|
||||
file->buffer_size = BUFSIZ;
|
||||
file->buffer_index = 0;
|
||||
}
|
||||
|
||||
void _init_stdio()
|
||||
{
|
||||
for (size_t i = 0; i < FOPEN_MAX; i++)
|
||||
init_closed_file(&s_files[i]);
|
||||
|
||||
s_files[STDIN_FILENO].fd = STDIN_FILENO;
|
||||
s_files[STDIN_FILENO].mode = O_RDONLY;
|
||||
|
||||
s_files[STDOUT_FILENO].fd = STDOUT_FILENO;
|
||||
s_files[STDOUT_FILENO].mode = O_WRONLY;
|
||||
|
||||
s_files[STDERR_FILENO].fd = STDERR_FILENO;
|
||||
s_files[STDERR_FILENO].mode = O_WRONLY;
|
||||
s_files[STDERR_FILENO].buffer_type = _IONBF;
|
||||
|
||||
s_files[STDDBG_FILENO].fd = STDDBG_FILENO;
|
||||
s_files[STDDBG_FILENO].mode = O_WRONLY;
|
||||
}
|
||||
|
||||
void clearerr(FILE* file)
|
||||
{
|
||||
ScopeLock _(file);
|
||||
@@ -79,7 +107,7 @@ int fclose(FILE* file)
|
||||
ScopeLock _(file);
|
||||
(void)fflush(file);
|
||||
int ret = (close(file->fd) == -1) ? EOF : 0;
|
||||
file = {};
|
||||
init_closed_file(file);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
|
||||
char** environ;
|
||||
|
||||
extern void init_malloc();
|
||||
extern void _init_malloc();
|
||||
extern void _init_stdio();
|
||||
extern "C" void _init_libc(char** _environ)
|
||||
{
|
||||
init_malloc();
|
||||
_init_malloc();
|
||||
_init_stdio();
|
||||
|
||||
if (!_environ)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user