Supporting emulated tls

Johannes Pfau nospam at example.com
Fri Mar 23 03:02:44 PDT 2012


Am Fri, 23 Mar 2012 05:48:46 +0100
schrieb "Martin Nowak" <dawg at dawgfoto.de>:

> On Mon, 19 Mar 2012 16:57:29 +0100, Johannes Pfau
> <nospam at example.com> wrote:
> 
> > The only way to access all loaded library is dl_iterate_phdr. But
> > I'm not sure if it provides all necessary information.
> 
> Yes it does.
> 
> The drawback is that it eagerly allocates the TLS block.
> https://github.com/dawgfoto/druntime/blob/SharedRuntime/src/rt/dso.d#L408
> https://github.com/dawgfoto/druntime/blob/SharedRuntime/src/rt/dso.d#L459

As written in some comment in your code, we can avoid eager allocation
using some architecture dependent 'hacks'. I think we'd have to
* get the thread pointer (architecture specific)
* find the dtv (there's only variant 1 / variant 2?)
* access the correct dtv entry (C library dependent?)
* check if the dtv entry is initialized (C library dependent?)

For FreeBSD step 3/4 means checking if dtv[index + 1] == 0. It's
probably the same for most other C libraries. The tricky part is that
we have to check first if the dtv is big enough for the current index.
For FreeBSD, this is easy again, dtv[1] contains the size of the dtv.
But that's probably nonstandard.

All this is not hard to do, but quite system specific. Normal Desktop
OS probably don't need this optimization. For systems which benifit
from it, adding it shouldn't be too difficult.

In case you're interested, the FreeBSD linker source is here (BSD
licensed, of course):
http://www.freebsd.org/cgi/cvsweb.cgi/src/libexec/rtld-elf/rtld.c?rev=1.196;content-type=text%2Fplain
search for tls_get_addr_slow, __tls_get_addr, dtv, tls 


More information about the D.gnu mailing list