From 836a8acb7ba6b9a9707ab3ae706b4973f4b6389f Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Sat, 29 Aug 2015 20:11:53 +0200 Subject: [PATCH] properly call global destructors --- libretro/Retro68Runtime.h | 1 + libretro/relocate.c | 13 +++++++++++++ libretro/start.c | 1 + 3 files changed, 15 insertions(+) diff --git a/libretro/Retro68Runtime.h b/libretro/Retro68Runtime.h index 8a8f96bdad..b3091be5b7 100644 --- a/libretro/Retro68Runtime.h +++ b/libretro/Retro68Runtime.h @@ -48,6 +48,7 @@ void Retro68Relocate(); void Retro68CallConstructors(); +void Retro68CallDestructors(); void Retro68FreeGlobals(); #define RETRO68_RELOCATE() RETRO68_CALL_UNRELOCATED(Retro68Relocate,()) diff --git a/libretro/relocate.c b/libretro/relocate.c index 0b09081e96..4fb13b00aa 100644 --- a/libretro/relocate.c +++ b/libretro/relocate.c @@ -223,6 +223,19 @@ void Retro68CallConstructors() } } +void Retro68CallDestructors() +{ + char *p = (char*)&__fini_section; + char *e = (char*)&__fini_section_end; + p += 2; + while( p < e ) + { + (*(voidFunction)(*(long*)p))(); + p += 6; + } +} + + void Retro68FreeGlobals() { if(relocState.bssPtr != (Ptr) -1) diff --git a/libretro/start.c b/libretro/start.c index f4d514c57a..e4f47dd89d 100644 --- a/libretro/start.c +++ b/libretro/start.c @@ -33,6 +33,7 @@ int main(int argc, char* argv[]); void _start() { RETRO68_RELOCATE(); + atexit(&Retro68CallDestructors); Retro68CallConstructors(); int result;