@@ -438,10 +438,39 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
438
438
static BOOL
439
439
RegisterExceptionHandling (PMEMORYMODULE module )
440
440
{
441
- PIMAGE_DATA_DIRECTORY pDir = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_EXCEPTION );
442
- PIMAGE_RUNTIME_FUNCTION_ENTRY pEntry = (PIMAGE_RUNTIME_FUNCTION_ENTRY )(module -> codeBase + pDir -> VirtualAddress );
443
- UINT count = (pDir -> Size / sizeof (IMAGE_RUNTIME_FUNCTION_ENTRY )) - 1 ;
444
- return RtlAddFunctionTable (pEntry , count , (DWORD64 )module -> codeBase );
441
+ #ifdef _WIN64
442
+ PRUNTIME_FUNCTION pEntry ;
443
+ DWORD count ;
444
+ PIMAGE_DATA_DIRECTORY pDir = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_EXCEPTION );
445
+ if (pDir -> Size == 0 ) {
446
+ return TRUE;
447
+ }
448
+
449
+ pEntry = (PRUNTIME_FUNCTION )(module -> codeBase + pDir -> VirtualAddress );
450
+ count = (pDir -> Size / sizeof (RUNTIME_FUNCTION ));
451
+ return RtlAddFunctionTable (pEntry , count , (DWORD64 ) module -> codeBase );
452
+ #else
453
+ // TODO(fancycode): Support 32bit exception handling.
454
+ return TRUE;
455
+ #endif
456
+ }
457
+
458
+ static BOOL
459
+ UnregisterExceptionHandling (PMEMORYMODULE module )
460
+ {
461
+ #ifdef _WIN64
462
+ PRUNTIME_FUNCTION pEntry ;
463
+ PIMAGE_DATA_DIRECTORY pDir = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_EXCEPTION );
464
+ if (pDir -> Size == 0 ) {
465
+ return TRUE;
466
+ }
467
+
468
+ pEntry = (PRUNTIME_FUNCTION )(module -> codeBase + pDir -> VirtualAddress );
469
+ return RtlDeleteFunctionTable (pEntry );
470
+ #else
471
+ // TODO(fancycode): Support 32bit exception handling.
472
+ return TRUE;
473
+ #endif
445
474
}
446
475
447
476
static BOOL
@@ -734,6 +763,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
734
763
goto error ;
735
764
}
736
765
766
+ // register exception handling table so "try { } catch ( ) { }"" works
737
767
if (!RegisterExceptionHandling (result )) {
738
768
goto error ;
739
769
}
@@ -880,6 +910,8 @@ void MemoryFreeLibrary(HMEMORYMODULE mod)
880
910
(* DllEntry )((HINSTANCE )module -> codeBase , DLL_PROCESS_DETACH , 0 );
881
911
}
882
912
913
+ UnregisterExceptionHandling (module );
914
+
883
915
free (module -> nameExportsTable );
884
916
if (module -> modules != NULL ) {
885
917
// free previously opened libraries
0 commit comments