diff --git a/src/dso.c b/src/dso.c index 0dc7215..8b572ba 100644 --- a/src/dso.c +++ b/src/dso.c @@ -1,4 +1,4 @@ -#include +#include /* We don't support shared object loading, so provide a single handle */ __attribute__((visibility("hidden"))) @@ -13,12 +13,18 @@ struct dtor { void *d; }; -static struct dtor _dtors[ATEXIT_MAX]; +static struct dtor *_dtors; static int _dtor_count = 0; +__attribute__((constructor)) +static void alloc_dtors(void) +{ + _dtors = malloc(ATEXIT_MAX * sizeof *_dtors); +} + int __cxa_atexit(void (*f)(void *), void *p, void *d) { - if(_dtor_count >= ATEXIT_MAX) + if(!_dtors || _dtor_count >= ATEXIT_MAX) return 1; _dtors[_dtor_count++] = (struct dtor){ f, p, d }; return 0;