Implement __cxa_atexit()

This commit is contained in:
avdgrinten 2015-12-05 19:00:01 +01:00
parent ef0e1dfd42
commit 7e10bb1469
5 changed files with 72 additions and 15 deletions

View file

@ -10,10 +10,14 @@ $c_OBJECT_PATHS := $(addprefix $($c_OBJDIR)/,$($c_OBJECTS))
$c_CXX := x86_64-managarm-g++
$c_CPPFLAGS := -std=c++11 -Wall
$c_CPPFLAGS += -I$(FRIGG_PATH)/include
$c_CPPFLAGS += -I$(TREE_PATH)/libc/generic/ansi/include
$c_CPPFLAGS += -I$(TREE_PATH)/libc/generic/posix/include
$c_CPPFLAGS += -I$(TREE_PATH)/libc/compilers/gcc/include
$c_CPPFLAGS += -I$(TREE_PATH)/libc/platform/x86_64-managarm/include
$c_CPPFLAGS += -DFRIGG_HAVE_LIBC
$c_CXXFLAGS := $($c_CPPFLAGS) -fPIC -O2
$c_CXXFLAGS += -fno-rtti -fno-exceptions
$c_TARGETS := clean-$c install-$c $($c_OBJECT_PATHS)

View file

@ -1,8 +1,35 @@
#include <mlibc/ensure.h>
extern "C" int __cxa_atexit(void (*handler)(void *), void *argument, void *dso) {
__ensure(!"Not implemented");
__builtin_unreachable();
#pragma GCC visibility push(hidden)
#include <mlibc/cxx-support.hpp>
#include <mlibc/frigg-alloc.hpp>
#include <frigg/initializer.hpp>
#include <frigg/vector.hpp>
struct ExitHandler {
void (*function)(void *);
void *argument;
void *dsoTag;
};
frigg::LazyInitializer<frigg::Vector<ExitHandler, MemoryAllocator>> exitHandlers;
#pragma GCC visibility pop
extern "C" int __cxa_atexit(void (*function)(void *), void *argument, void *dso_tag) {
// FIXME: initialize this from a global library guard constructor
__ensure(memoryAllocator);
if(!exitHandlers)
exitHandlers.initialize(*memoryAllocator);
ExitHandler handler;
handler.function = function;
handler.argument = argument;
handler.dsoTag = dso_tag;
exitHandlers->push(handler);
return 0;
}

View file

@ -0,0 +1,25 @@
#ifndef MLIBC_FRIGG_ALLOC
#define MLIBC_FRIGG_ALLOC
#pragma GCC visibility push(hidden)
#include <frigg/initializer.hpp>
#include <frigg/memory.hpp>
struct VirtualAllocator {
public:
uintptr_t map(size_t length);
void unmap(uintptr_t address, size_t length);
};
typedef frigg::SlabAllocator<VirtualAllocator, frigg::TicketLock> MemoryAllocator;
extern VirtualAllocator virtualAllocator;
extern frigg::LazyInitializer<MemoryAllocator> memoryAllocator;
#pragma GCC visibility pop
#endif // MLIBC_FRIGG_ALLOC

View file

@ -16,6 +16,10 @@ LibraryGuard::LibraryGuard() {
//__mlibc_initMalloc();
}
// __dso_handle is usually defined in crtbeginS.o
// Since we link with -nostdlib we have to manually define it here
__attribute__ (( visibility("hidden") )) int __dso_handle;
extern "C" int main(int argc, char *argv[], char *env[]);
extern "C" void __mlibc_entry() {

View file

@ -4,23 +4,23 @@
#include <mlibc/ensure.h>
#include <mlibc/cxx-support.hpp>
#include <mlibc/frigg-alloc.hpp>
#pragma GCC visibility push(hidden)
#include <frigg/initializer.hpp>
#include <frigg/memory.hpp>
#include <hel.h>
#include <hel-syscalls.h>
struct VirtualAllocator {
public:
uintptr_t map(size_t length);
// --------------------------------------------------------
// Globals
// --------------------------------------------------------
void unmap(uintptr_t address, size_t length);
};
VirtualAllocator virtualAllocator;
frigg::LazyInitializer<MemoryAllocator> memoryAllocator;
typedef frigg::SlabAllocator<VirtualAllocator, frigg::TicketLock> MemoryAllocator;
// --------------------------------------------------------
// VirtualAllocator
// --------------------------------------------------------
uintptr_t VirtualAllocator::map(size_t length) {
assert((length % 0x1000) == 0);
@ -38,9 +38,6 @@ void VirtualAllocator::unmap(uintptr_t address, size_t length) {
HEL_CHECK(helUnmapMemory(kHelNullHandle, (void *)address, length));
}
VirtualAllocator virtualAllocator;
frigg::LazyInitializer<MemoryAllocator> memoryAllocator;
#pragma GCC visibility pop
void __mlibc_initMalloc() {