1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:47:47 +00:00

LibC: Run constructors on process startup.

Cooperate with the compiler to generate and execute the _init_array list
of constructor functions on userspace program statup. This took two days
to get working, my goodness. :^)
This commit is contained in:
Andreas Kling 2019-03-27 12:48:21 +01:00
parent f1a2cb0882
commit 23bb276fcd
22 changed files with 101 additions and 61 deletions

1
LibC/.gitignore vendored
View file

@ -1,4 +1,5 @@
*.o
*.no
*.ao
*.d
libc.a

View file

@ -40,17 +40,16 @@ LIBC_OBJS = \
poll.o \
locale.o \
arpa/inet.o \
netdb.o \
crt0.o
netdb.o
ASM_OBJS = setjmp.no
ASM_OBJS = setjmp.no crti.ao crtn.ao
CPP_OBJS = $(AK_OBJS) $(WIDGETS_OBJS) $(LIBC_OBJS)
LIBRARY = libc.a
STANDARD_FLAGS = -std=c++17
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
FLAVOR_FLAGS = -fno-exceptions -fno-rtti -fno-sized-deallocation
OPTIMIZATION_FLAGS = -Os
INCLUDE_FLAGS = -I.. -I.
@ -60,8 +59,14 @@ CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STA
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
AS = i686-pc-serenity-as
all: $(LIBRARY)
all: $(LIBRARY) startfiles
startfiles:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o crt0.o -c crt0.cpp
cp crti.ao crti.o
cp crtn.ao crtn.o
$(LIBRARY): $(CPP_OBJS) $(ASM_OBJS)
@echo "LIB $@"; $(AR) rcs $@ $(CPP_OBJS) $(ASM_OBJS)
@ -72,6 +77,9 @@ $(LIBRARY): $(CPP_OBJS) $(ASM_OBJS)
%.no: %.asm
@echo "NASM $@"; nasm -f elf -o $@ $<
%.ao: %.S
@echo "AS $@"; $(AS) -o $@ $<
-include $(OBJS:%.o=%.d)
clean:

View file

@ -1,6 +1,7 @@
#include <LibC/SharedBuffer.h>
#include <unistd.h>
#include <stdio.h>
#include <AK/kmalloc.h>
RetainPtr<SharedBuffer> SharedBuffer::create(pid_t peer, int size)
{

View file

@ -10,17 +10,31 @@ int errno;
char** environ;
//bool __environ_is_malloced;
void __malloc_init();
void __stdio_init();
void __libc_init()
{
void __malloc_init();
__malloc_init();
void __stdio_init();
__stdio_init();
}
int _start(int argc, char** argv, char** env)
{
errno = 0;
environ = env;
//__environ_is_malloced = false;
__stdio_init();
__malloc_init();
__libc_init();
extern void _init();
_init();
extern void (*__init_array_start[])(int, char**, char**) __attribute__((visibility("hidden")));
extern void (*__init_array_end[])(int, char**, char**) __attribute__((visibility("hidden")));
const size_t size = __init_array_end - __init_array_start;
for (size_t i = 0; i < size; i++)
(*__init_array_start[i])(argc, argv, env);
int status = main(argc, argv);
@ -37,4 +51,8 @@ int _start(int argc, char** argv, char** env)
assert(false);
}
void __cxa_atexit()
{
}
}

9
LibC/crti.S Normal file
View file

@ -0,0 +1,9 @@
.global _init
.section .init
_init:
push %ebp
.global _fini
.section .fini
_fini:
push %ebp

7
LibC/crtn.S Normal file
View file

@ -0,0 +1,7 @@
.section .init
pop %ebp
ret
.section .fini
pop %ebp
ret

View file

@ -10,3 +10,5 @@ cp arpa/*.h ../Root/usr/include/arpa/
cp netinet/*.h ../Root/usr/include/netinet/
cp libc.a ../Root/usr/lib/
cp crt0.o ../Root/usr/lib/
cp crti.ao ../Root/usr/lib/crti.o
cp crtn.ao ../Root/usr/lib/crtn.o