On 2013-01-07 09:04, Walter Bright wrote:

Please nail down what is necessary first. (BTW, I don't know how the
compiler can tell what image an address comes from. Remember, shared
libraries are loaded at runtime, not compile time.)

I've done some investigation. Currently DMD inserts a call to the __tls_get_addr function when accessing a TLS variable. This is implemented in druntime.

In Mac OS X 10.7 it works similar but instead of inserting a call to __tls_get_addr there's a struct looking like this (written in D) :

struct TLVDescriptor
{
    void* function (TLVDescriptor*) thunk;
    size_t key;
    size_t offset;
}

The dynamic linker will iterate all loaded images and extract the section containing the TLS data. I guess this section consists of a list of TLVDescriptor*. The dynamic linker will set the "thunk" to a function "tlv_get_addrs", implemented in assembly in the dynamic linker. It will set the key to a key created by "pthread_key_create". It will also map the image with this key. This key is same for all TLVDescriptor of a given image.

Instead of calling "__tls_get_addr" I think that the compiler will need to call the thunk passing in the TLVDescriptor itself to the thunk.

The "tlv_get_addrs" function will then extract the key and from that key it can get the image the address belongs to.

Does that make any sens? Is that something the DMD could do, that is call the thunk instead of "__tls_get_addr".?

--
/Jacob Carlborg

Reply via email to