Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions src/rt/sections_osx_x86.d
Original file line number Diff line number Diff line change
Expand Up @@ -112,83 +112,6 @@ void scanTLSRanges(void[]* rng, scope void delegate(void* pbeg, void* pend) noth
dg(rng.ptr, rng.ptr + rng.length);
}

// NOTE: The Mach-O object file format does not allow for thread local
// storage declarations. So instead we roll our own by putting tls
// into the __tls_data and the __tlscoal_nt sections.
//
// This function is called by the code emitted by the compiler. It
// is expected to translate an address into the TLS static data to
// the corresponding address in the TLS dynamic per-thread data.

// NB: the compiler mangles this function as '___tls_get_addr' even though it is extern(D)
extern(D) void* ___tls_get_addr( void* p )
{
immutable off = tlsOffset(p);
auto tls = getTLSBlockAlloc();
assert(off < tls.length);
return tls.ptr + off;
}

private:

__gshared pthread_key_t _tlsKey;

size_t tlsOffset(void* p)
in
{
assert(_sections._tlsImage[0].ptr !is null ||
_sections._tlsImage[1].ptr !is null);
}
body
{
// NOTE: p is an address in the TLS static data emitted by the
// compiler. If it isn't, something is disastrously wrong.
immutable off0 = cast(size_t)(p - _sections._tlsImage[0].ptr);
if (off0 < _sections._tlsImage[0].length)
{
return off0;
}
immutable off1 = cast(size_t)(p - _sections._tlsImage[1].ptr);
if (off1 < _sections._tlsImage[1].length)
{
size_t sz = (_sections._tlsImage[0].length + 15) & ~cast(size_t)15;
return sz + off1;
}
assert(0);
}

ref void[] getTLSBlock() nothrow @nogc
{
auto pary = cast(void[]*)pthread_getspecific(_tlsKey);
if (pary is null)
{
pary = cast(void[]*).calloc(1, (void[]).sizeof);
if (pthread_setspecific(_tlsKey, pary) != 0)
{
import core.stdc.stdio;
perror("pthread_setspecific failed with");
assert(0);
}
}
return *pary;
}

ref void[] getTLSBlockAlloc()
{
auto pary = &getTLSBlock();
if (!pary.length)
{
auto imgs = _sections._tlsImage;
immutable sz0 = (imgs[0].length + 15) & ~cast(size_t)15;
immutable sz2 = sz0 + imgs[1].length;
auto p = .malloc(sz2);
memcpy(p, imgs[0].ptr, imgs[0].length);
memcpy(p + sz0, imgs[1].ptr, imgs[1].length);
*pary = p[0 .. sz2];
}
return *pary;
}

__gshared SectionGroup _sections;

extern (C) void sections_osx_onAddImage(in mach_header* h, intptr_t slide)
Expand Down