Fix ctor/dtor handling in mlibc

This commit is contained in:
avdgrinten 2016-11-01 16:09:40 +01:00
parent 63340ec1f7
commit 9ecd6a8236
6 changed files with 99 additions and 6 deletions

View file

@ -1,15 +1,45 @@
$c_SRCDIR = $(TREE_PATH)/$c/src
$c_HEADERDIR := $(TREE_PATH)/$c/include
$c_OBJDIR := $(BUILD_PATH)/$c/obj
$c_HEADERS := stdint.h \
mlibc/null.h mlibc/size_t.h mlibc/wchar_t.h
$c_TARGETS := install-headers-$c
$c_OBJECTS := initfini.o
$c_OBJECT_PATHS := $(addprefix $($c_OBJDIR)/,$($c_OBJECTS))
.PHONY: install-headers-$c
$c_EXTRA_OBJECTS := mlibc_begin.o mlibc_end.o
$c_EXTRA_OBJECT_PATHS := $(addprefix $($c_OBJDIR)/,$($c_EXTRA_OBJECTS))
$c_AS := x86_64-managarm-as
$c_CXX := x86_64-managarm-g++
$c_CPPFLAGS := -std=c++14 -Wall
$c_CXXFLAGS := $($c_CPPFLAGS) -fPIC -O2
$c_CXXFLAGS += -fno-builtin -fno-rtti -fno-exceptions
$c_TARGETS := clean-$c install-headers-$c $($c_OBJECT_PATHS) $($c_EXTRA_OBJECT_PATHS)
.PHONY: clean-$c install-headers-$c
clean-$c:
rm -f $($c_OBJECT_PATHS) $($c_EXTRA_OBJECT_PATHS)
rm -f $($c_OBJECT_PATHS:%.o=%.d) $($c_EXTRA_OBJECT_PATHS:%.o=%.d)
install-headers-$c:
mkdir -p $(SYSROOT_PATH)/usr/include/mlibc
for f in $($c_HEADERS); do \
install -p $($c_HEADERDIR)/$$f $(SYSROOT_PATH)/usr/include/$$f; done
$($c_OBJDIR):
mkdir -p $@
$($c_OBJDIR)/%.o: $($c_SRCDIR)/%.S | $($c_OBJDIR)
$($c_AS) -o $@ $($c_ASFLAGS) $<
$($c_OBJDIR)/%.o: $($c_SRCDIR)/%.cpp | $($c_OBJDIR)
$($c_CXX) -c -o $@ $($c_CXXFLAGS) $<
$($c_CXX) $($c_CPPFLAGS) -MM -MP -MF $(@:%.o=%.d) -MT "$@" -MT "$(@:%.o=%.d)" $<
-include $($c_OBJECT_PATHS:%.o=%.d)

View file

@ -0,0 +1,9 @@
extern "C" [[ gnu::visibility("hidden") ]] void __mlibc_do_ctors() {
}
extern "C" [[ gnu::visibility("hidden") ]] void __mlibc_do_dtors() {
}

View file

@ -0,0 +1,27 @@
.section .data
.hidden __dso_handle
.global __dso_handle
__dso_handle:
.quad __dso_handle
.section .init
.hidden _init
.global _init
_init:
.section .fini
.hidden _fini
.global _fini
_fini:
.section .ctors
.hidden __CTOR_LIST__
.global __CTOR_LIST__
__CTOR_LIST__:
.section .dtors
.hidden __DTOR_LIST__
.global __DTOR_LIST__
__DTOR_LIST__:

View file

@ -0,0 +1,20 @@
.hidden __mlibc_do_ctors
.hidden __mlibc_do_dtors
.section .init
call __mlibc_do_ctors
ret
.section .fini
call __mlibc_do_dtors
ret
.section .ctors
.hidden __CTOR_END__
__CTOR_END__:
.section .dtors
.hidden __DTOR_END__
__DTOR_END__:

View file

@ -3,7 +3,7 @@ $c_BINDIR := $(BUILD_PATH)/$c/bin
$c_LIBRARY_OBJS :=
$c_LDFLAGS := -nodefaultlibs
$c_LDFLAGS := -nostdlib
$c_TARGETS := clean-$c gen-$c install-$c install-headers-$c $($c_BINDIR)/libc.so
@ -32,6 +32,8 @@ clean-$c: clean-$c/generic/lsb
install-headers-$c: install-headers-$c/generic/lsb
$(call include_dir,$c/compilers/gcc)
$c_LIBRARY_OBJS += $($c/compilers/gcc_OBJECT_PATHS)
clean-$c: clean-$c/compilers/gcc
install-headers-$c: install-headers-$c/compilers/gcc
$(call include_dir,$c/machine/x86_64)
@ -49,13 +51,16 @@ $(call include_dir,$c/frigg-bindings)
$c_LIBRARY_OBJS += $($c/frigg-bindings_OBJECT_PATHS)
clean-$c: clean-$c/frigg-bindings
$c_BEGIN = $c/compilers/gcc/obj/mlibc_begin.o
$c_END = $c/compilers/gcc/obj/mlibc_end.o
$c_LIBS := -l:ld-init.so
$($c_BINDIR):
mkdir -p $@
$($c_BINDIR)/libc.so: $($c_LIBRARY_OBJS) | $($c_BINDIR)
x86_64-managarm-g++ -shared -o $@ $($c_LDFLAGS) $($c_LIBRARY_OBJS) $($c_LIBS)
$($c_BINDIR)/libc.so: $($c_LIBRARY_OBJS) $($c_BEGIN) $($c_END) | $($c_BINDIR)
x86_64-managarm-g++ -shared -o $@ $($c_LDFLAGS) $($c_BEGIN) $($c_LIBRARY_OBJS) $($c_LIBS) $($c_END)
install-$c: install-headers-$c
mkdir -p $(SYSROOT_PATH)/usr/lib

View file

@ -5,7 +5,9 @@ $c_OBJDIR := $(BUILD_PATH)/$c/obj
$c_LIBRARY_OBJS := $($c_OBJDIR)/dummy.o
$c_LDFLAGS := -nodefaultlibs
# FIXME: We do have crtbegin/crtend here so global ctors do not work!
# Either build libm after libgcc or include a private version of crtbegin/crtend.
$c_LDFLAGS := -nostdlib
$c_TARGETS := clean-$c install-$c $($c_BINDIR)/libm.so