      if (addr == NULL) {
          delete ce;
          return 0;
-@@ -195,6 +317,11 @@
+@@ -195,6 +319,11 @@
  
  uintptr_t _EREMOVE(const void *epc_lin_addr)
  {
 +    {
-+        std::lock_guard<std::mutex> lock(thread_to_tcs_lock);
-+        thread_to_tcs.clear();
++        std::lock_guard<std::mutex> lock(*thread_to_tcs_lock);
++        thread_to_tcs->clear();
 +    }
 +
      CEnclaveMngr *mngr = CEnclaveMngr::get_instance();
      CEnclaveSim *ce = mngr->get_enclave(epc_lin_addr);
  
-@@ -232,6 +359,11 @@
+@@ -232,6 +361,11 @@
          // xbx contains the address of a TCS
          tcs = reinterpret_cast<tcs_t*>(xbx);
  
 +        {
-+            std::lock_guard<std::mutex> lock(thread_to_tcs_lock);
-+            thread_to_tcs[pthread_self()] = tcs;
++            std::lock_guard<std::mutex> lock(*thread_to_tcs_lock);
++            (*thread_to_tcs)[pthread_self()] = tcs;
 +        }
 +
          // Is TCS pointer page-aligned?

 #include <stdio.h>
 #include <stdlib.h>
 
@@ -71,6 +78,119 @@
 
 #define mcp_same_size(dst_ptr, src_ptr, size) memcpy_s(dst_ptr, size, src_ptr, size)
 
+// A map that stores all the signal handlers registered prior to entering the
+// enclave.
+static std::unordered_map<int, std::unique_ptr<struct sigaction>>
+    registered_signal_handlers;
+
+// A lock that protects |registered_signal_handlers| map.
+static std::mutex registered_signal_handlers_lock;
+
+// A map that stores the pthread ids with their corresponding tcs.
+static std::unordered_map<pthread_t, tcs_t *> thread_to_tcs;
+
+// A lock that protects |thread_to_tcs| map.
+static std::mutex thread_to_tcs_lock;
+
+// If a signal arrives in simulation mode while the thread is running inside the
+// enclave, the handler does not have access to the thread local variables, as

+        // Stores the registered handler in |registered_signal_handlers| map,
+        // and registers |switch_tls_and_handle_signal| as the handler, which
+        // switches the thread register and invokes the registered handler.
+        if (registered_signal_handlers->find(signum)
+            == registered_signal_handlers->end())
+        {
+            if (((oldact.sa_flags & SA_SIGINFO) && oldact.sa_sigaction)
+                || ((!(oldact.sa_flags & SA_SIGINFO)) && oldact.sa_handler
+                    && oldact.sa_handler != SIG_DFL))
+            {
+                (*registered_signal_handlers)[signum] =
+                    std::unique_ptr<struct sigaction>(new struct sigaction(oldact));
+                struct sigaction act;
+                act.sa_flags = oldact.sa_flags;
