Check-in [c92b6d4398]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Handle DLL_PROCESS_DETACH
Timelines: family | ancestors | bug-94e3289b79
Files: files | file ages | folders
SHA3-256: c92b6d4398d575043a1327726e4329a30d049d6d4eaa1f78ce869ca7459f650c
User & Date: jan.nijtmans 2025-01-25 23:29:48.521
Context
2025-01-25
23:29
Handle DLL_PROCESS_DETACH Closed-Leaf check-in: c92b6d4398 user: jan.nijtmans tags: bug-94e3289b79
2025-01-24
09:13
Value of [ThreadVar -index] depends on static/shared/gcc build (might be 2, 4 or 6). So make testcas... check-in: 4fc8999b76 user: jan.nijtmans tags: tip-709
Changes
Unified Diff Ignore Whitespace Patch
Changes to win/Makefile.in.
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677

tclTestMain.${OBJEXT}: tclAppInit.c
	$(CC) -c $(CC_SWITCHES) -DTCL_TEST -DUNICODE -D_UNICODE $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinInit.${OBJEXT}: tclWinInit.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinLoad.${OBJEXT}: tclWinLoad.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl -DUNICODE -D_UNICODE $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinPipe.${OBJEXT}: tclWinPipe.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinReg.${OBJEXT}: tclWinReg.c
	$(CC) -c $(CC_SWITCHES) $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)







|







663
664
665
666
667
668
669
670
671
672
673
674
675
676
677

tclTestMain.${OBJEXT}: tclAppInit.c
	$(CC) -c $(CC_SWITCHES) -DTCL_TEST -DUNICODE -D_UNICODE $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinInit.${OBJEXT}: tclWinInit.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinLoad.${OBJEXT}: tclWinLoad.c MemoryModule.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl -DUNICODE -D_UNICODE $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinPipe.${OBJEXT}: tclWinPipe.c
	$(CC) -c $(CC_SWITCHES) -DBUILD_tcl $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)

tclWinReg.${OBJEXT}: tclWinReg.c
	$(CC) -c $(CC_SWITCHES) $(EXTFLAGS) @DEPARG@ $(CC_OBJNAME)
Changes to win/MemoryModule.c.
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    if (!FinalizeSection(module, &sectionData)) {
        return FALSE;
    }
    return TRUE;
}

static BOOL
ExecuteTLS(PMEMORYMODULE module)
{
#ifndef TCL_LOAD_FROM_MEMORY
    unsigned char *codeBase = module->codeBase;
    PIMAGE_TLS_DIRECTORY tls;
    PIMAGE_TLS_CALLBACK* callback;
#endif /* !TCL_LOAD_FROM_MEMORY */

    PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_TLS);
    if (directory->VirtualAddress == 0) {
        return TRUE;
    }

#ifdef TCL_LOAD_FROM_MEMORY
    return FALSE;
#else
    tls = (PIMAGE_TLS_DIRECTORY) (codeBase + directory->VirtualAddress);
    callback = (PIMAGE_TLS_CALLBACK *) tls->AddressOfCallBacks;
    if (callback) {
        while (*callback) {
            (*callback)((LPVOID) codeBase, DLL_PROCESS_ATTACH, NULL);
            callback++;
        }
    }
    return TRUE;
#endif /* TCL_LOAD_FROM_MEMORY */
}








|

|










|






|







354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    if (!FinalizeSection(module, &sectionData)) {
        return FALSE;
    }
    return TRUE;
}

static BOOL
ExecuteTLS(PMEMORYMODULE module, DWORD dwReason, PVOID pv)
{
#if !defined(TCL_LOAD_FROM_MEMORY) || (TCL_LOAD_FROM_MEMORY > 1)
    unsigned char *codeBase = module->codeBase;
    PIMAGE_TLS_DIRECTORY tls;
    PIMAGE_TLS_CALLBACK* callback;
#endif /* !TCL_LOAD_FROM_MEMORY */

    PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_TLS);
    if (directory->VirtualAddress == 0) {
        return TRUE;
    }

#if defined(TCL_LOAD_FROM_MEMORY) && (TCL_LOAD_FROM_MEMORY < 2)
    return FALSE;
#else
    tls = (PIMAGE_TLS_DIRECTORY) (codeBase + directory->VirtualAddress);
    callback = (PIMAGE_TLS_CALLBACK *) tls->AddressOfCallBacks;
    if (callback) {
        while (*callback) {
            (*callback)((LPVOID) codeBase, dwReason, pv);
            callback++;
        }
    }
    return TRUE;
#endif /* TCL_LOAD_FROM_MEMORY */
}

757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
    // mark memory pages depending on section headers and release
    // sections that are marked as "discardable"
    if (!FinalizeSections(result)) {
        goto error;
    }

    // TLS callbacks are executed BEFORE the main loading
    if (!ExecuteTLS(result)) {
        goto error;
    }

    // get entry point of loaded library
    if (result->headers->OptionalHeader.AddressOfEntryPoint != 0) {
        if (result->isDLL) {
            DllEntryProc DllEntry = (DllEntryProc)(LPVOID)(code + result->headers->OptionalHeader.AddressOfEntryPoint);







|







757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
    // mark memory pages depending on section headers and release
    // sections that are marked as "discardable"
    if (!FinalizeSections(result)) {
        goto error;
    }

    // TLS callbacks are executed BEFORE the main loading
    if (!ExecuteTLS(result, DLL_PROCESS_ATTACH, NULL)) {
        goto error;
    }

    // get entry point of loaded library
    if (result->headers->OptionalHeader.AddressOfEntryPoint != 0) {
        if (result->isDLL) {
            DllEntryProc DllEntry = (DllEntryProc)(LPVOID)(code + result->headers->OptionalHeader.AddressOfEntryPoint);
892
893
894
895
896
897
898




899
900
901
902
903
904
905
    if (module == NULL) {
        return;
    }
    if (module->initialized) {
        // notify library about detaching from process
        DllEntryProc DllEntry = (DllEntryProc)(LPVOID)(module->codeBase + module->headers->OptionalHeader.AddressOfEntryPoint);
        (*DllEntry)((HINSTANCE)module->codeBase, DLL_PROCESS_DETACH, 0);




    }

    free(module->nameExportsTable);
    if (module->modules != NULL) {
        // free previously opened libraries
        int i;
        for (i=0; i<module->numModules; i++) {







>
>
>
>







892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
    if (module == NULL) {
        return;
    }
    if (module->initialized) {
        // notify library about detaching from process
        DllEntryProc DllEntry = (DllEntryProc)(LPVOID)(module->codeBase + module->headers->OptionalHeader.AddressOfEntryPoint);
        (*DllEntry)((HINSTANCE)module->codeBase, DLL_PROCESS_DETACH, 0);
    }
    // notify TLS about detaching from process
    if (!ExecuteTLS(result, DLL_PROCESS_DETACH, NULL)) {
        goto error;
    }

    free(module->nameExportsTable);
    if (module->modules != NULL) {
        // free previously opened libraries
        int i;
        for (i=0; i<module->numModules; i++) {
Changes to win/configure.
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
else case e in #(
  e) tcl_ok=no ;;
esac
fi

if test "$tcl_ok" != "no" ; then

printf "%s\n" "#define TCL_LOAD_FROM_MEMORY 1" >>confdefs.h

fi


#--------------------------------------------------------------------
# Perform additinal compiler tests.
#--------------------------------------------------------------------







|







5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
else case e in #(
  e) tcl_ok=no ;;
esac
fi

if test "$tcl_ok" != "no" ; then

printf "%s\n" "#define TCL_LOAD_FROM_MEMORY 2" >>confdefs.h

fi


#--------------------------------------------------------------------
# Perform additinal compiler tests.
#--------------------------------------------------------------------
Changes to win/configure.ac.
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#	MemoryModule support - Tip 709
#--------------------------------------------------------------------
AC_ARG_ENABLE(memorymodule,
    AS_HELP_STRING([--enable-memorymodule],
	[load dll's from a vfs in memory in stead of copying to a temporary directory (default: off)]),
    [tcl_ok=$enableval], [tcl_ok=no])
if test "$tcl_ok" != "no" ; then
    AC_DEFINE(TCL_LOAD_FROM_MEMORY, 1, [Are we building with MemoryModule enabled?])
fi
AC_SUBST(TCL_LOAD_FROM_MEMORY)

#--------------------------------------------------------------------
# Perform additinal compiler tests.
#--------------------------------------------------------------------








|







213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#	MemoryModule support - Tip 709
#--------------------------------------------------------------------
AC_ARG_ENABLE(memorymodule,
    AS_HELP_STRING([--enable-memorymodule],
	[load dll's from a vfs in memory in stead of copying to a temporary directory (default: off)]),
    [tcl_ok=$enableval], [tcl_ok=no])
if test "$tcl_ok" != "no" ; then
    AC_DEFINE(TCL_LOAD_FROM_MEMORY, 2, [Are we building with MemoryModule enabled?])
fi
AC_SUBST(TCL_LOAD_FROM_MEMORY)

#--------------------------------------------------------------------
# Perform additinal compiler tests.
#--------------------------------------------------------------------