[fpc-pascal] StackTop and StackBottom are bogus in a Win32/64 Dll

Pierre Free Pascal pierre at freepascal.org
Mon Jul 9 09:22:00 CEST 2012

  If I got that right, the problem is linked to the
essence of a DLL (or shared library):
A shared library can be used by any program.
Thus the stack size might be different in location and size
depending on that executable that loaded the DLL.

  So the only solution would be that the information is passed
by the executable to the DLL.
  Unfortunately, I don't think that this is done by standard
call to DLL_ENTRY callback.
  This means that you could still add something special 
to the DLL_ENTRY callback to pass this information, but it would mean
that use of your DLL by some program that was not written by you
would probably not pass that information, and you then
must restrict the DLL usage to only the programs you write
or must request that specific entry parameter as a
DLL usage requirement.
  This will become even more complicated if you
start using multiple threads.
  StackTop and StackBottom are threadvars,
which is a free pascal specific construct consisting in:
an assembler label with two fields:
 First a pointer size integer that holds the offset within the
Thread Variable Block of the thread specific value of the variable,
followed by a second block of size of the threadvar considered
(also pointer size in those two cases) containing the
value for unthreaded executable...
  This also means that we do need to pass to the
DLL the multithreaded state of the executable...

  While it probably isn't really difficult to code such
information, the main problem is that it will limit the
possible use of your DLLs to Free Pascal generated executables.

Pierre Muller

> -----Message d'origine-----
> De : fpc-pascal-bounces at lists.freepascal.org [mailto:fpc-pascal-
> bounces at lists.freepascal.org] De la part de Mark Morgan Lloyd
> Envoyé : vendredi 6 juillet 2012 18:16
> À : fpc-pascal at lists.freepascal.org
> Objet : Re: [fpc-pascal] StackTop and StackBottom are bogus in a Win32/64
> Dll
> OBones wrote:
> > Hello all,
> >
> > I'm writing Dlls here and am also doing low level analysis of the stack
> > and its content.
> > To do that, I use the StackTop and StackBottom global variables which
> > used to work fine in an exe file.
> > Problem is, when in a DLL, StackTop is always zero and StackBottom is
> > the exact opposite of StackLength.
> > I thus looked as to where this might come from, and discovered that it
> > comes from the fact that StackTop is only ever calculated from within
> > Exe_entry which obviously is not called for a DLL.
> >
> > Is anyone else seeing this?
> I've not got into that level of detail, but I've noted that in Linux
> (and possibly Solaris) you have to turn off stack checking when
> generating a shared library (otherwise when the main program calls into
> the library it's unhappy) but not in the main program (i.e. the library
> can call into the main program and is always happy) which I think is
> consistent with what you're saying.
> --
> Mark Morgan Lloyd
> markMLl .AT. telemetry.co .DOT. uk
> [Opinions above are the author's, not those of his employers or
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

More information about the fpc-pascal mailing list