How do values get into the %gs:0x0 location?

Amittai Aviram amittai.aviram@yale.edu
Wed Aug 26 15:35:00 GMT 2009


> Hi Amittai,
>
> Have you looked the function _dl_allocate_tls (it is from glibc, but
> it is compiled to the dynamic linker) ? It not only allocates, but
> fills the tls.
>
> And you are right, the content of $gs:0x0 is the same address of
> $gs:0x0. The applications makes an indirect access to the TLS static
> block using this pointer (if you compile with the option
> -mno-tls-direct-seg-refs all the access to TLS data are made this
> way).
>


Thanks a lot, Eduardo!  I found the source code for _dl_allocate_tls  
and related routines in <glibc_root>/elf/dl-tls.c .  But how does the  
linker get the information about which objects to put into the first  
TLS area at startup?  In particular, I know that my program has to  
store pointers to at least three locale-related objects --  
_nl_C_LC_CTYPE, _nl_C_LC_MONETARY, and _nl_C_LC_NUMERIC.  So I have a  
couple of questions about that --

The compiler knows the offsets within the TLS section (for the startup  
thread) for those objects, since it refers to them by offset from %gs: 
0x0.  But where does it get those offsets?  The compiler must build a  
table at compile time mapping locale objects (and perhaps other  
objects needed at startup) to offsets, or else there must already be a  
table somewhere that maps such objects to standard offsets.

The linker knows the actual virtual addresses of the needed objects,  
but how does it know which objects are needed?  Let's assume static  
linkage -- which is what I am using.  How does the linker know which  
addresses to have the system store at the offsets known at compile time?

Although I found Ulrich Drepper's paper "ELF Handling for Thread-Local  
Storage" helpful for understanding the general organization and  
handling of the TLS section, I couldn't find the answers to my  
questions there.  Did I miss something?


Thanks a lot!


Amittai Aviram
Graduate Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com

>



More information about the Libc-help mailing list