os_query_stackBottom() how does it work?

Lionello Lunesu lio at lunesu.remove.com
Mon May 8 07:16:32 PDT 2006


mclysenk at mtu.edu wrote:
> In article <e3mf16$1g83$1 at digitaldaemon.com>, Chad J says...
>> /**********************************************
>>  * Determine "bottom" of stack (actually the top on Win32 systems).
>>  */
>>
>> void *os_query_stackBottom()
>> {
>>     asm
>>     {
>> 	naked			;
>> 	mov	EAX,FS:4	;
>> 	ret			;
>>     }
>> }
>>
> 
> That snippet of code is pulling out the bottom of the stack from the Thread
> Execution Block (TEB).  The TEB is stored in process' memory and is also
> mirrored in the FS segment.  FS:0 is the top of the structured exception handler
> chain, FS:4 is the bottom of the stack and FS:8 is the top of the stack.


Isn't the bottom of the stack the same as ESP? If so, couldn't the code 
be replaced by something like

void* bla(){ int x; return &x; }

or using alloca or something?

I suppose it's the GC that needs the stack bottom/top, but why would it 
be interested in anything before ESP?

L.



More information about the D.gnu mailing list