[fpc-devel] Manual reload of a DLL snapshot (with relocations) causes multiple AV

Gennadiy Poryev core at barvinok.net
Fri Jan 6 19:10:24 CET 2012

> > First, I'd like to know why calling of DLL entry point anew does not
> > re-initialize _all_ RTL internals regardless of what was remembered
> > prior to taking the snapshot. Is it just assuming good behavior of
> > system loader that carefully zeroes data segment while I don't?
> The point is: why should a DLL that is used normally take special care of this?
> The normal lifetime of a DLL is
> * DLL is loaded by LoadLibrary
> * Windows calls DLL entrypoint with PROCESS_ATTACH
> * DLL is used
> * DLL is unloaded by FreeLibrary
> * Windows calls DLL entrypoint with PROCESS_DETACH

Actually PROCESS_DETACH call happens before DLL is unloaded.
But my point is that I take snapshot of the DLL image before all this happens, actually right after the call of its entry point with PROCESS_ATTACH.
Therefore, for a DLL it appears as if PROCESS_ATTACH is called once again, only this time with different imagebase (i.e. hInstance) and relocs adjusted accordingly. And I don't see why it should not work this way.

> If now the process loads that DLL again after a PROCESS_DETACH it runs the
> complete PROCESS_ATTACH again.

I wish it was that easy. But there is no reasonable way to take snapshot image between PROCESS_DETACH and actual freeing of the memory.

> Thinking about this a bit... it might be that there is still the TLS value set to a
> value <> Nil. Thus the RTL will reference the old values which are no longer
> valid. I'd suggest you to take a look at %fpcdir%/rtl/win/systhrd.inc and there
> SysRelocateThreadVar. It's just a guess though.

If so, how can I make sure these values are properly initialized if I call dll entry point again?

More information about the fpc-devel mailing list