diff --git a/bl2u/bl2u_main.c b/bl2u/bl2u_main.c
index a7e3fb916ef85d3eb50c5aafa36fa43990c96630..b29d57e9db6fd5ffd2fd0ec28663112880718699 100644
--- a/bl2u/bl2u_main.c
+++ b/bl2u/bl2u_main.c
@@ -17,6 +17,7 @@
 #include <platform_def.h>
 #include <stdint.h>
 
+
 /*******************************************************************************
  * This function is responsible to:
  * Load SCP_BL2U if platform has defined SCP_BL2U_BASE
diff --git a/drivers/auth/mbedtls/mbedtls_common.c b/drivers/auth/mbedtls/mbedtls_common.c
index c048d005aae395f580dd4f60a7441b596bdf5ffc..64dc1967deb2f77a70b550f60a45b10badc11e86 100644
--- a/drivers/auth/mbedtls/mbedtls_common.c
+++ b/drivers/auth/mbedtls/mbedtls_common.c
@@ -5,6 +5,7 @@
  */
 
 #include <debug.h>
+#include <stdlib.h>
 
 /* mbed TLS headers */
 #include <mbedtls/memory_buffer_alloc.h>
@@ -23,6 +24,12 @@
 #endif
 static unsigned char heap[MBEDTLS_HEAP_SIZE];
 
+static void cleanup(void)
+{
+	ERROR("EXIT from BL2\n");
+	panic();
+}
+
 /*
  * mbed TLS initialization function
  */
@@ -31,6 +38,9 @@ void mbedtls_init(void)
 	static int ready;
 
 	if (!ready) {
+		if (atexit(cleanup))
+			panic();
+
 		/* Initialize the mbed TLS heap */
 		mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
 
diff --git a/lib/libc/exit.c b/lib/libc/exit.c
index afc3f934390d04ec4c121461a5ace26bdd91e78d..b2fde9ca290d2d7696cb4f5bff5d3b227e5a83c2 100644
--- a/lib/libc/exit.c
+++ b/lib/libc/exit.c
@@ -4,11 +4,23 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <debug.h>
 #include <stdlib.h>
 
-void exit(int v)
+static void (*exitfun)(void);
+
+void exit(int status)
 {
-	ERROR("EXIT\n");
-	panic();
+	if (exitfun)
+		(*exitfun)();
+	for (;;)
+		;
+}
+
+int atexit(void (*fun)(void))
+{
+	if (exitfun)
+		return -1;
+	exitfun = fun;
+
+	return 0;
 }