From 61cefe693a047e22ca44752eafb9eb9e2e65409f Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Fri, 1 May 2020 19:59:20 -0600 Subject: [PATCH] cs: support `raco exe --embed-dlls` on Windows Uses a MemoryModule PR by @DaLynX to fix a setjmp/longjmp interaction with embedded DLLs. --- racket/collects/compiler/embed.rkt | 1 - racket/src/start/MemoryModule.c | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/racket/collects/compiler/embed.rkt b/racket/collects/compiler/embed.rkt index e17ca833a7..e0be5f2e26 100644 --- a/racket/collects/compiler/embed.rkt +++ b/racket/collects/compiler/embed.rkt @@ -1563,7 +1563,6 @@ dest mred?)))))) (define embed-dlls? (and (eq? 'windows (cross-system-type)) - (eq? 'racket (cross-system-type 'vm)) (let ([m (assq 'embed-dlls? aux)]) (and m (cdr m))))) (define embedded-dlls-box (and embed-dlls? (box null))) diff --git a/racket/src/start/MemoryModule.c b/racket/src/start/MemoryModule.c index d3925bf7bf..7cd277bb31 100644 --- a/racket/src/start/MemoryModule.c +++ b/racket/src/start/MemoryModule.c @@ -435,6 +435,17 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta) return TRUE; } +#ifdef _WIN64 +static BOOL +RegisterExceptionHandling(PMEMORYMODULE module) +{ + PIMAGE_DATA_DIRECTORY pDir = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION); + PIMAGE_RUNTIME_FUNCTION_ENTRY pEntry = (PIMAGE_RUNTIME_FUNCTION_ENTRY)(module->codeBase + pDir->VirtualAddress); + UINT count = (pDir->Size / sizeof(IMAGE_RUNTIME_FUNCTION_ENTRY)) - 1; + return RtlAddFunctionTable(pEntry, count, (DWORD64)module->codeBase); +} +#endif + static BOOL BuildImportTable(PMEMORYMODULE module) { @@ -725,6 +736,12 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, goto error; } +#ifdef _WIN64 + if (!RegisterExceptionHandling(result)) { + goto error; + } +#endif + // mark memory pages depending on section headers and release // sections that are marked as "discardable" if (!FinalizeSections(result)) {