Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 019217a

Browse files
authored
Merge pull request #2320 from kinke/sections_upstream
Slightly refactor rt.sections_elf_shared [NFC] merged-on-behalf-of: Petar Kirov <ZombineDev@users.noreply.github.com>
2 parents e807e29 + 90e6976 commit 019217a

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

src/rt/sections_elf_shared.d

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ private:
122122
Array!(DSO*) _deps; // D libraries needed by this DSO
123123
void* _handle; // corresponding handle
124124
}
125+
126+
// get the TLS range for the executing thread
127+
void[] tlsRange() const nothrow @nogc
128+
{
129+
return getTLSRange(_tlsMod, _tlsSize);
130+
}
125131
}
126132

127133
/****
@@ -302,7 +308,7 @@ version (Shared)
302308
// update the _tlsRange for the executing thread
303309
void updateTLSRange() nothrow @nogc
304310
{
305-
_tlsRange = getTLSRange(_pdso._tlsMod, _pdso._tlsSize);
311+
_tlsRange = _pdso.tlsRange();
306312
}
307313
}
308314
Array!(ThreadDSO) _loadedDSOs;
@@ -404,15 +410,14 @@ extern(C) void _d_dso_registry(CompilerDSOData* data)
404410
* thread with a refCnt of 1 and call the TlsCtors.
405411
*/
406412
immutable ushort refCnt = 1, addCnt = 0;
407-
auto tlsRng = getTLSRange(pdso._tlsMod, pdso._tlsSize);
408-
_loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, tlsRng));
413+
_loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
409414
}
410415
}
411416
else
412417
{
413418
foreach (p; _loadedDSOs) assert(p !is pdso);
414419
_loadedDSOs.insertBack(pdso);
415-
_tlsRanges.insertBack(getTLSRange(pdso._tlsMod, pdso._tlsSize));
420+
_tlsRanges.insertBack(pdso.tlsRange());
416421
}
417422

418423
// don't initialize modules before rt_init was called (see Bugzilla 11378)
@@ -509,8 +514,7 @@ version (Shared)
509514
foreach (dep; pdso._deps)
510515
incThreadRef(dep, false);
511516
immutable ushort refCnt = 1, addCnt = incAdd ? 1 : 0;
512-
auto tlsRng = getTLSRange(pdso._tlsMod, pdso._tlsSize);
513-
_loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, tlsRng));
517+
_loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
514518
pdso._moduleGroup.runTlsCtors();
515519
}
516520
}
@@ -623,22 +627,22 @@ void freeDSO(DSO* pdso) nothrow @nogc
623627
version (Shared)
624628
{
625629
@nogc nothrow:
626-
link_map* linkMapForHandle(void* handle) nothrow @nogc
630+
link_map* linkMapForHandle(void* handle)
627631
{
628632
link_map* map;
629633
dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0 || assert(0);
630634
return map;
631635
}
632636

633-
link_map* exeLinkMap(link_map* map) nothrow @nogc
637+
link_map* exeLinkMap(link_map* map)
634638
{
635639
assert(map);
636640
while (map.l_prev !is null)
637641
map = map.l_prev;
638642
return map;
639643
}
640644

641-
DSO* dsoForHandle(void* handle) nothrow @nogc
645+
DSO* dsoForHandle(void* handle)
642646
{
643647
DSO* pdso;
644648
!pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
@@ -648,23 +652,23 @@ version (Shared)
648652
return pdso;
649653
}
650654

651-
void setDSOForHandle(DSO* pdso, void* handle) nothrow @nogc
655+
void setDSOForHandle(DSO* pdso, void* handle)
652656
{
653657
!pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
654658
assert(handle !in _handleToDSO);
655659
_handleToDSO[handle] = pdso;
656660
!pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
657661
}
658662

659-
void unsetDSOForHandle(DSO* pdso, void* handle) nothrow @nogc
663+
void unsetDSOForHandle(DSO* pdso, void* handle)
660664
{
661665
!pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
662666
assert(_handleToDSO[handle] == pdso);
663667
_handleToDSO.remove(handle);
664668
!pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
665669
}
666670

667-
void getDependencies(in ref dl_phdr_info info, ref Array!(DSO*) deps) nothrow @nogc
671+
void getDependencies(in ref dl_phdr_info info, ref Array!(DSO*) deps)
668672
{
669673
// get the entries of the .dynamic section
670674
ElfW!"Dyn"[] dyns;
@@ -716,7 +720,7 @@ version (Shared)
716720
}
717721
}
718722

719-
void* handleForName(const char* name) nothrow @nogc
723+
void* handleForName(const char* name)
720724
{
721725
auto handle = .dlopen(name, RTLD_NOLOAD | RTLD_LAZY);
722726
if (handle !is null) .dlclose(handle); // drop reference count
@@ -770,59 +774,51 @@ void scanSegments(in ref dl_phdr_info info, DSO* pdso) nothrow @nogc
770774

771775
/**************************
772776
* Input:
773-
* result where the output is to be written; dl_phdr_info is a Linux struct
777+
* result where the output is to be written; dl_phdr_info is an OS struct
774778
* Returns:
775779
* true if found, and *result is filled in
776780
* References:
777781
* http://linux.die.net/man/3/dl_iterate_phdr
778782
*/
779-
version (linux) bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
783+
bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
780784
{
781-
static struct DG { const(void)* addr; dl_phdr_info* result; }
785+
version (linux) enum IterateManually = true;
786+
else version (NetBSD) enum IterateManually = true;
787+
else enum IterateManually = false;
782788

783-
extern(C) int callback(dl_phdr_info* info, size_t sz, void* arg) nothrow @nogc
789+
static if (IterateManually)
784790
{
785-
auto p = cast(DG*)arg;
786-
if (findSegmentForAddr(*info, p.addr))
791+
static struct DG { const(void)* addr; dl_phdr_info* result; }
792+
793+
extern(C) int callback(dl_phdr_info* info, size_t sz, void* arg) nothrow @nogc
787794
{
788-
if (p.result !is null) *p.result = *info;
789-
return 1; // break;
795+
auto p = cast(DG*)arg;
796+
if (findSegmentForAddr(*info, p.addr))
797+
{
798+
if (p.result !is null) *p.result = *info;
799+
return 1; // break;
800+
}
801+
return 0; // continue iteration
790802
}
791-
return 0; // continue iteration
792-
}
793803

794-
auto dg = DG(addr, result);
804+
auto dg = DG(addr, result);
795805

796-
/* Linux function that walks through the list of an application's shared objects and
797-
* calls 'callback' once for each object, until either all shared objects
798-
* have been processed or 'callback' returns a nonzero value.
799-
*/
800-
return dl_iterate_phdr(&callback, &dg) != 0;
801-
}
802-
else version (FreeBSD) bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
803-
{
804-
return !!_rtld_addr_phdr(addr, result);
805-
}
806-
else version (NetBSD) bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
807-
{
808-
static struct DG { const(void)* addr; dl_phdr_info* result; }
809-
810-
extern(C) int callback(dl_phdr_info* info, size_t sz, void* arg) nothrow @nogc
806+
/* OS function that walks through the list of an application's shared objects and
807+
* calls 'callback' once for each object, until either all shared objects
808+
* have been processed or 'callback' returns a nonzero value.
809+
*/
810+
return dl_iterate_phdr(&callback, &dg) != 0;
811+
}
812+
else version (FreeBSD)
811813
{
812-
auto p = cast(DG*)arg;
813-
if (findSegmentForAddr(*info, p.addr))
814-
{
815-
if (p.result !is null) *p.result = *info;
816-
return 1; // break;
817-
}
818-
return 0; // continue iteration
814+
return !!_rtld_addr_phdr(addr, result);
819815
}
820-
auto dg = DG(addr, result);
821-
return dl_iterate_phdr(&callback, &dg) != 0;
822-
}
823-
else version (DragonFlyBSD) bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
824-
{
825-
return !!_rtld_addr_phdr(addr, result);
816+
else version (DragonFlyBSD)
817+
{
818+
return !!_rtld_addr_phdr(addr, result);
819+
}
820+
else
821+
static assert(0, "unimplemented");
826822
}
827823

828824
/*********************************

0 commit comments

Comments
 (0)