Skip to content

Commit

Permalink
Implement shared library support in druntime
Browse files Browse the repository at this point in the history
  • Loading branch information
jpf91 committed Aug 11, 2014
1 parent aaf31c1 commit 1dc78aa
Show file tree
Hide file tree
Showing 16 changed files with 1,783 additions and 432 deletions.
9 changes: 6 additions & 3 deletions libphobos/libdruntime/__entrypoint.di
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ extern(C):
what the linker script says.
*/

size_t _tlsstart = 3;
size_t _tlsend = void;

version(linux) {}
else
{
size_t _tlsstart = 3;
size_t _tlsend = void;
}

/* The D main() function supplied by the user's program
Expand Down
27 changes: 27 additions & 0 deletions libphobos/libdruntime/__sharedlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stddef.h>

typedef struct
{
size_t _version; // currently 1
void** _slot; // can be used to store runtime data
void** _minfo_beg, **_minfo_end; // array of modules in this object file
void* _deh_beg, *_deh_end; // array of exception handling data
} CompilerDSOData;

void _d_dso_registry(CompilerDSOData* data);
extern void* __start_dminfo;
extern void* __stop_dminfo;

static void* slot;

__attribute__ ((constructor, destructor, used)) static void _dso_start()
{
CompilerDSOData data;
data._version = 1;
data._slot = &slot;
data._minfo_beg = &__start_dminfo;
data._minfo_end = &__stop_dminfo;
data._deh_beg = NULL;
data._deh_end = NULL;
_d_dso_registry(&data);
}
97 changes: 0 additions & 97 deletions libphobos/libdruntime/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,6 @@ version( Windows )
extern (Windows) alias uint function(void*) btex_fptr;
extern (C) uintptr_t _beginthreadex(void*, uint, btex_fptr, void*, uint, uint*);

version (MinGW)
{
import gcc.builtins;

// NOTE: The memory between the addresses of _tls_start and _tls_end
// is the storage for thread-local data in MinGW. Both of
// these are defined in tlssup.c.
extern (C)
{
extern int _tls_start;
extern int _tls_end;
}

alias _tls_start _tlsstart;
alias _tls_end _tlsend;
}

//
// Entry point for Windows threads
//
Expand Down Expand Up @@ -246,12 +229,6 @@ else version( Posix )
version( GNU )
{
import gcc.builtins;

extern (C)
{
extern size_t _tlsstart;
extern size_t _tlsend;
}
}

//
Expand All @@ -276,12 +253,6 @@ else version( Posix )
obj.m_main.bstack = getStackBottom();
obj.m_main.tstack = obj.m_main.bstack;
obj.m_tlsgcdata = rt.tlsgc.init();
version (GNU)
{
auto pstart = cast(void*) &_tlsstart;
auto pend = cast(void*) &_tlsend;
obj.m_tls = pstart[0 .. pend - pstart];
}

obj.m_isRunning = true;
Thread.setThis( obj );
Expand Down Expand Up @@ -1202,13 +1173,6 @@ private:
{
m_call = Call.NO;
m_curr = &m_main;

version (GNU)
{
auto pstart = cast(void*) &_tlsstart;
auto pend = cast(void*) &_tlsend;
m_tls = pstart[0 .. pend - pstart];
}
}


Expand Down Expand Up @@ -1370,10 +1334,6 @@ private:
Context m_main;
Context* m_curr;
bool m_lock;
version (GNU)
{
void[] m_tls; // spans implicit thread local storage
}
rt.tlsgc.Data* m_tlsgcdata;

version( Windows )
Expand Down Expand Up @@ -1668,37 +1628,6 @@ private:
}

// These must be kept in sync with core/thread.di
version (GNU)
{
version (D_LP64)
{
version (Windows)
static assert(__traits(classInstanceSize, Thread) == 312);
else version (OSX)
static assert(__traits(classInstanceSize, Thread) == 320);
else version (Solaris)
static assert(__traits(classInstanceSize, Thread) == 176);
else version (Posix)
static assert(__traits(classInstanceSize, Thread) == 184);
else
static assert(0, "Platform not supported.");
}
else
{
static assert((void*).sizeof == 4); // 32-bit

version (Windows)
static assert(__traits(classInstanceSize, Thread) == 128);
else version (OSX)
static assert(__traits(classInstanceSize, Thread) == 128);
else version (Posix)
static assert(__traits(classInstanceSize, Thread) == 92);
else
static assert(0, "Platform not supported.");

}
}
else
version (D_LP64)
{
version (Windows)
Expand Down Expand Up @@ -1890,13 +1819,6 @@ extern (C) Thread thread_attachThis()
assert( thisThread.m_tmach != thisThread.m_tmach.init );
}

version (GNU)
{
auto pstart = cast(void*) &_tlsstart;
auto pend = cast(void*) &_tlsend;
thisThread.m_tls = pstart[0 .. pend - pstart];
}

Thread.add( thisThread );
Thread.add( thisContext );
if( Thread.sm_main !is null )
Expand Down Expand Up @@ -1946,12 +1868,6 @@ version( Windows )
{
thisThread.m_hndl = GetCurrentThreadHandle();
thisThread.m_tlsgcdata = rt.tlsgc.init();
version (GNU)
{
auto pstart = cast(void*) &_tlsstart;
auto pend = cast(void*) &_tlsend;
thisThread.m_tls = pstart[0 .. pend - pstart];
}
Thread.setThis( thisThread );
}
else
Expand All @@ -1960,16 +1876,6 @@ version( Windows )
impersonate_thread(addr,
{
thisThread.m_tlsgcdata = rt.tlsgc.init();
version (GNU)
{
auto pstart = cast(void*) &_tlsstart;
auto pend = cast(void*) &_tlsend;
auto pos = GetTlsDataAddress( thisThread.m_hndl );
if( pos ) // on x64, threads without TLS happen to exist
thisThread.m_tls = pos[0 .. pend - pstart];
else
thisThread.m_tls = [];
}
Thread.setThis( thisThread );
});
}
Expand Down Expand Up @@ -2603,8 +2509,6 @@ private void scanAllTypeImpl( scope ScanAllThreadsTypeFn scan, void* curStackTop
// would make portability annoying because it only makes sense on Windows.
scan( ScanType.stack, t.m_reg.ptr, t.m_reg.ptr + t.m_reg.length );
}
version (GNU)
scan( ScanType.tls, t.m_tls.ptr, t.m_tls.ptr + t.m_tls.length );

if (t.m_tlsgcdata !is null)
rt.tlsgc.scan(t.m_tlsgcdata, (p1, p2) => scan(ScanType.tls, p1, p2));
Expand Down Expand Up @@ -4506,7 +4410,6 @@ private:
}

// These must be kept in sync with core/thread.di
version (GNU) {} else
version (D_LP64)
{
version (Windows)
Expand Down
21 changes: 0 additions & 21 deletions libphobos/libdruntime/core/thread.di
Original file line number Diff line number Diff line change
Expand Up @@ -401,27 +401,6 @@ private:
}

// These must be kept in sync with core/thread.d
version (GNU)
{
version (D_LP64)
{
version (Windows) enum ThreadSize = 312;
else version (OSX) enum ThreadSize = 320;
else version (Solaris) enum ThreadSize = 176;
else version (Posix) enum ThreadSize = 184;
else static assert(0, "Platform not supported.");
}
else
{
static assert((void*).sizeof == 4); // 32-bit

version (Windows) enum ThreadSize = 128;
else version (OSX) enum ThreadSize = 128;
else version (Posix) enum ThreadSize = 92;
else static assert(0, "Platform not supported.");
}
}
else
version (D_LP64)
{
version (Windows) enum ThreadSize = 296;
Expand Down
7 changes: 6 additions & 1 deletion libphobos/libdruntime/gcc/deh.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import gcc.builtins;
import core.memory;
import core.stdc.stdlib;

// Not used in GDC but declaration required by rt/sections.d
struct FuncTable
{
}

extern(C)
{
int _d_isbaseof(ClassInfo, ClassInfo);
Expand All @@ -42,7 +47,7 @@ version(GNU_SEH_Exceptions)
ExceptionCollidedUnwind
}

//Pointer types. We're lazy, exact definition in MinGW/winnt.h
// Pointer types. We're lazy, exact definition in MinGW/winnt.h
alias PEXCEPTION_RECORD = void*;
alias PCONTEXT = void*;
alias PDISPATCHER_CONTEXT = void*;
Expand Down
3 changes: 3 additions & 0 deletions libphobos/libdruntime/rt/dmain2.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module rt.dmain2;
private
{
import rt.memory;
import rt.sections;
import rt.util.string;
import core.stdc.stddef;
import core.stdc.stdlib;
Expand Down Expand Up @@ -163,6 +164,7 @@ extern (C) int rt_init()

try
{
initSections();
gc_init();
initStaticDataGC();
rt_moduleCtor();
Expand Down Expand Up @@ -193,6 +195,7 @@ extern (C) int rt_term()
thread_joinAll();
rt_moduleDtor();
gc_term();
finiSections();
return 1;
}
catch (Throwable t)
Expand Down
Loading

0 comments on commit 1dc78aa

Please sign in to comment.