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
Notes: sideshowbarker 2024-07-19 14:55:50 +09:00
22 changed files with 101 additions and 61 deletions

View file

@ -52,6 +52,8 @@ void* mmx_memcpy(void* dest, const void* src, size_t len)
return dest;
}
#ifdef KERNEL
static inline uint32_t divq(uint64_t n, uint32_t d)
{
uint32_t n1 = n >> 32;
@ -144,5 +146,6 @@ uint64_t __udivmoddi4(uint64_t n, uint64_t d, uint64_t* r)
return q;
}
#endif
}

View file

@ -46,36 +46,6 @@ void* kmalloc_eternal(size_t size)
}
void* operator new(size_t size)
{
return kmalloc(size);
}
void* operator new[](size_t size)
{
return kmalloc(size);
}
void operator delete(void* ptr)
{
return kfree(ptr);
}
void operator delete[](void* ptr)
{
return kfree(ptr);
}
void operator delete(void* ptr, size_t)
{
return kfree(ptr);
}
void operator delete[](void* ptr, size_t)
{
return kfree(ptr);
}
#else
extern "C" {

View file

@ -1,5 +1,9 @@
#pragma once
#ifndef KERNEL
#include <new>
#endif
#if defined(SERENITY) && defined(KERNEL)
#define AK_MAKE_ETERNAL \
public: \
@ -23,6 +27,31 @@ void kfree(void* ptr);
}
#ifdef KERNEL
inline void* operator new(size_t, void* p) { return p; }
inline void* operator new[](size_t, void* p) { return p; }
#else
inline void* operator new(size_t size)
{
return kmalloc(size);
}
inline void operator delete(void* ptr)
{
return kfree(ptr);
}
inline void* operator new[](size_t size)
{
return kmalloc(size);
}
inline void operator delete[](void* ptr)
{
return kfree(ptr);
}
#endif
#endif

View file

@ -13,8 +13,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -15,8 +15,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -14,7 +14,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
LD = i686-pc-serenity-g++
AR = i686-pc-serenity-ar
LDFLAGS = -L../../LibC -L../../LibGUI

View file

@ -22,7 +22,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
LD = i686-pc-serenity-g++
AR = i686-pc-serenity-ar
LDFLAGS = -L../../LibC -L../../LibGUI

View file

@ -13,8 +13,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -16,8 +16,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -14,8 +14,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -13,8 +13,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
AR = i686-pc-serenity-ar
LD = i686-pc-serenity-g++
LDFLAGS = -L../../LibC -L../../LibGUI
all: $(APP)

View file

@ -10,8 +10,6 @@ $make_cmd -C ../LibC && \
$make_cmd -C ../LibM clean && \
$make_cmd -C ../LibM && \
(cd ../LibM && ./install.sh) && \
$make_cmd -C ../LibM clean && \
$make_cmd -C ../LibM clean && \
$make_cmd -C ../Servers/LookupServer clean && \
$make_cmd -C ../Servers/LookupServer && \
$make_cmd -C ../Servers/WindowServer clean && \

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

View file

@ -8,14 +8,14 @@ STANDARD_FLAGS = -std=c++17
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
OPTIMIZATION_FLAGS = -Os
INCLUDE_FLAGS = -I../../ -I. -I../../LibC
INCLUDE_FLAGS = -I.. -I../.. -I. -I../../LibC
LDFLAGS = -L../../LibC
DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
LD = i686-pc-serenity-g++
AR = i686-pc-serenity-ar
all: $(APP)

View file

@ -36,7 +36,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
LD = i686-pc-serenity-g++
AR = i686-pc-serenity-ar
all: $(APP)

View file

@ -86,7 +86,7 @@ APPS = \
ARCH_FLAGS =
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. -I../LibC
@ -94,7 +94,7 @@ DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-ld
LD = i686-pc-serenity-g++
AR = i686-pc-serenity-r
LDFLAGS = -L ../LibC