1 // ELF-specific support for sections with shared libraries.
2 // Copyright (C) 2019-2024 Free Software Foundation, Inc.
4 // GCC is free software; you can redistribute it and/or modify it under
5 // the terms of the GNU General Public License as published by the Free
6 // Software Foundation; either version 3, or (at your option) any later
9 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
23 module gcc.sections.elf;
25 version (MIPS32) version = MIPS_Any;
26 version (MIPS64) version = MIPS_Any;
27 version (RISCV32) version = RISCV_Any;
28 version (RISCV64) version = RISCV_Any;
29 version (S390) version = IBMZ_Any;
30 version (SystemZ) version = IBMZ_Any;
32 version (CRuntime_Glibc) enum SharedELF = true;
33 else version (CRuntime_Musl) enum SharedELF = true;
34 else version (FreeBSD) enum SharedELF = true;
35 else version (NetBSD) enum SharedELF = true;
36 else version (OpenBSD) enum SharedELF = true;
37 else version (DragonFlyBSD) enum SharedELF = true;
38 else version (CRuntime_UClibc) enum SharedELF = true;
39 else version (Solaris) enum SharedELF = true;
40 else enum SharedELF = false;
41 static if (SharedELF):
44 import core.stdc.config;
45 import core.stdc.stdio;
46 import core.stdc.stdlib : calloc, exit, free, malloc, EXIT_FAILURE;
47 import core.stdc.string : strlen;
50 import core.sys.linux.dlfcn;
51 import core.sys.linux.elf;
52 import core.sys.linux.link;
54 else version (FreeBSD)
56 import core.sys.freebsd.dlfcn;
57 import core.sys.freebsd.sys.elf;
58 import core.sys.freebsd.sys.link_elf;
62 import core.sys.netbsd.dlfcn;
63 import core.sys.netbsd.sys.elf;
64 import core.sys.netbsd.sys.link_elf;
66 else version (OpenBSD)
68 import core.sys.openbsd.dlfcn;
69 import core.sys.openbsd.sys.elf;
70 import core.sys.openbsd.sys.link_elf;
72 else version (DragonFlyBSD)
74 import core.sys.dragonflybsd.dlfcn;
75 import core.sys.dragonflybsd.sys.elf;
76 import core.sys.dragonflybsd.sys.link_elf;
78 else version (Solaris)
80 import core.sys.solaris.dlfcn;
81 import core.sys.solaris.link;
82 import core.sys.solaris.sys.elf;
83 import core.sys.solaris.sys.link;
87 static assert(0, "unimplemented");
89 import core.sys.posix.pthread;
93 import core.internal.container.array;
94 import core.internal.container.hashtab;
97 import gcc.sections.common;
99 alias DSO SectionGroup;
102 static int opApply(scope int delegate(ref DSO) dg)
104 foreach (dso; _loadedDSOs)
106 if (auto res = dg(*dso))
112 static int opApplyReverse(scope int delegate(ref DSO) dg)
114 foreach_reverse (dso; _loadedDSOs)
116 if (auto res = dg(*dso))
122 @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
124 return _moduleGroup.modules;
127 @property ref inout(ModuleGroup) moduleGroup() inout return nothrow @nogc
132 @property inout(void[])[] gcRanges() inout nothrow @nogc
141 safeAssert(_moduleGroup.modules.length > 0, "No modules for DSO.");
142 safeAssert(_tlsMod || !_tlsSize, "Inconsistent TLS fields for DSO.");
145 ModuleGroup _moduleGroup;
146 Array!(void[]) _gcRanges;
152 Array!(void[]) _codeSegments; // array of code segments
153 Array!(DSO*) _deps; // D libraries needed by this DSO
154 void* _handle; // corresponding handle
157 // get the TLS range for the executing thread
158 void[] tlsRange() const nothrow @nogc
160 return getTLSRange(_tlsMod, _tlsSize);
165 * Boolean flag set to true while the runtime is initialized.
167 __gshared bool _isRuntimeInitialized;
171 * Gets called on program startup just before GC is initialized.
173 void initSections() nothrow @nogc
175 _isRuntimeInitialized = true;
180 * Gets called on program shutdown just after GC is terminated.
182 void finiSections() nothrow @nogc
184 _isRuntimeInitialized = false;
187 alias ScanDG = void delegate(void* pbeg, void* pend) nothrow;
191 import gcc.sections : pinLoadedLibraries, unpinLoadedLibraries,
192 inheritLoadedLibraries, cleanupLoadedLibraries;
195 * Called once per thread; returns array of thread local storage ranges
197 Array!(ThreadDSO)* initTLSRanges() @nogc nothrow
199 return &_loadedDSOs();
202 void finiTLSRanges(Array!(ThreadDSO)* tdsos) @nogc nothrow
204 // Nothing to do here. tdsos used to point to the _loadedDSOs instance
205 // in the dying thread's TLS segment and as such is not valid anymore.
206 // The memory for the array contents was already reclaimed in
207 // cleanupLoadedLibraries().
210 void scanTLSRanges(Array!(ThreadDSO)* tdsos, scope ScanDG dg) nothrow
219 foreach (ref tdso; *tdsos)
220 dg(tdso._tlsRange.ptr, tdso._tlsRange.ptr + tdso._tlsRange.length);
224 size_t sizeOfTLS() nothrow @nogc
226 auto tdsos = initTLSRanges();
228 foreach (ref tdso; *tdsos)
229 sum += tdso._tlsRange.length;
233 // interface for core.thread to inherit loaded libraries
234 pragma(mangle, gcc.sections.pinLoadedLibraries.mangleof)
235 void* pinLoadedLibraries() nothrow @nogc
237 auto res = cast(Array!(ThreadDSO)*)calloc(1, Array!(ThreadDSO).sizeof);
238 res.length = _loadedDSOs.length;
239 foreach (i, ref tdso; _loadedDSOs)
244 // Increment the dlopen ref for explicitly loaded libraries to pin them.
245 const success = .dlopen(linkMapForHandle(tdso._pdso._handle).l_name, RTLD_LAZY) !is null;
246 safeAssert(success, "Failed to increment dlopen ref.");
247 (*res)[i]._addCnt = 1; // new array takes over the additional ref count
253 pragma(mangle, gcc.sections.unpinLoadedLibraries.mangleof)
254 void unpinLoadedLibraries(void* p) nothrow @nogc
256 auto pary = cast(Array!(ThreadDSO)*)p;
257 // In case something failed we need to undo the pinning.
258 foreach (ref tdso; *pary)
262 auto handle = tdso._pdso._handle;
263 safeAssert(handle !is null, "Invalid library handle.");
271 // Called before TLS ctors are ran, copy over the loaded libraries
272 // of the parent thread.
273 pragma(mangle, gcc.sections.inheritLoadedLibraries.mangleof)
274 void inheritLoadedLibraries(void* p) nothrow @nogc
276 safeAssert(_loadedDSOs.empty, "DSOs have already been registered for this thread.");
277 _loadedDSOs.swap(*cast(Array!(ThreadDSO)*)p);
279 foreach (ref dso; _loadedDSOs)
281 // the copied _tlsRange corresponds to parent thread
282 dso.updateTLSRange();
286 // Called after all TLS dtors ran, decrements all remaining dlopen refs.
287 pragma(mangle, gcc.sections.cleanupLoadedLibraries.mangleof)
288 void cleanupLoadedLibraries() nothrow @nogc
290 foreach (ref tdso; _loadedDSOs)
292 if (tdso._addCnt == 0) continue;
294 auto handle = tdso._pdso._handle;
295 safeAssert(handle !is null, "Invalid DSO handle.");
296 for (; tdso._addCnt > 0; --tdso._addCnt)
300 // Free the memory for the array contents.
307 * Called once per thread; returns array of thread local storage ranges
309 Array!(void[])* initTLSRanges() nothrow @nogc
311 auto rngs = &_tlsRanges();
314 foreach (ref pdso; _loadedDSOs)
315 rngs.insertBack(pdso.tlsRange());
320 void finiTLSRanges(Array!(void[])* rngs) nothrow @nogc
325 void scanTLSRanges(Array!(void[])* rngs, scope ScanDG dg) nothrow
335 dg(rng.ptr, rng.ptr + rng.length);
339 size_t sizeOfTLS() nothrow @nogc
341 auto rngs = initTLSRanges();
354 * Array of thread local DSO metadata for all libraries loaded and
355 * initialized in this thread.
358 * A newly spawned thread will inherit these libraries.
360 * We use an array here to preserve the order of
361 * initialization. If that became a performance issue, we
362 * could use a hash table and enumerate the DSOs during
363 * loading so that the hash table values could be sorted when
369 static if (_pdso.sizeof == 8) uint _refCnt, _addCnt;
370 else static if (_pdso.sizeof == 4) ushort _refCnt, _addCnt;
371 else static assert(0, "unimplemented");
374 // update the _tlsRange for the executing thread
375 void updateTLSRange() nothrow @nogc
377 _tlsRange = _pdso.tlsRange();
380 @property ref Array!(ThreadDSO) _loadedDSOs() @nogc nothrow { static Array!(ThreadDSO) x; return x; }
383 * Set to true during rt_loadLibrary/rt_unloadLibrary calls.
388 * Hash table to map link_map* to corresponding DSO*.
389 * The hash table is protected by a Mutex.
391 __gshared pthread_mutex_t _handleToDSOMutex;
392 @property ref HashTab!(void*, DSO*) _handleToDSO() @nogc nothrow { __gshared HashTab!(void*, DSO*) x; return x; }
397 * Static DSOs loaded by the runtime linker. This includes the
398 * executable. These can't be unloaded.
400 @property ref Array!(DSO*) _loadedDSOs() @nogc nothrow { __gshared Array!(DSO*) x; return x; }
403 * Thread local array that contains TLS memory ranges for each
404 * library initialized in this thread.
406 @property ref Array!(void[]) _tlsRanges() @nogc nothrow { static Array!(void[]) x; return x; }
408 enum _rtLoading = false;
411 ///////////////////////////////////////////////////////////////////////////////
412 // Compiler to runtime interface.
413 ///////////////////////////////////////////////////////////////////////////////
416 * This data structure is generated by the compiler, and then passed to
419 struct CompilerDSOData
421 size_t _version; // currently 1
422 void** _slot; // can be used to store runtime data
423 immutable(object.ModuleInfo*)* _minfo_beg, _minfo_end; // array of modules in this object file
426 T[] toRange(T)(T* beg, T* end) { return beg[0 .. end - beg]; }
428 /* For each shared library and executable, the compiler generates code that
429 * sets up CompilerDSOData and calls _d_dso_registry().
430 * A pointer to that code is inserted into both the .ctors and .dtors
431 * segment so it gets called by the loader on startup and shutdown.
433 extern(C) void _d_dso_registry(CompilerDSOData* data)
435 // only one supported currently
436 safeAssert(data._version >= 1, "Incompatible compiler-generated DSO data version.");
438 // no backlink => register
439 if (*data._slot is null)
441 immutable firstDSO = _loadedDSOs.empty;
442 if (firstDSO) initLocks();
444 DSO* pdso = cast(DSO*).calloc(1, DSO.sizeof);
445 assert(typeid(DSO).initializer().ptr is null);
446 *data._slot = pdso; // store backlink in library record
448 pdso._moduleGroup = ModuleGroup(toRange(data._minfo_beg, data._minfo_end));
450 dl_phdr_info info = void;
451 const headerFound = findDSOInfoForAddr(data._slot, &info);
452 safeAssert(headerFound, "Failed to find image header.");
454 scanSegments(info, pdso);
458 auto handle = handleForAddr(data._slot);
460 getDependencies(info, pdso._deps);
461 pdso._handle = handle;
462 setDSOForHandle(pdso, pdso._handle);
466 /* This DSO was not loaded by rt_loadLibrary which
467 * happens for all dependencies of an executable or
468 * the first dlopen call from a C program.
469 * In this case we add the DSO to the _loadedDSOs of this
470 * thread with a refCnt of 1 and call the TlsCtors.
472 immutable ushort refCnt = 1, addCnt = 0;
473 _loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
478 foreach (p; _loadedDSOs)
479 safeAssert(p !is pdso, "DSO already registered.");
480 _loadedDSOs.insertBack(pdso);
481 _tlsRanges.insertBack(pdso.tlsRange());
484 // don't initialize modules before rt_init was called (see Bugzilla 11378)
485 if (_isRuntimeInitialized)
487 registerGCRanges(pdso);
488 // rt_loadLibrary will run tls ctors, so do this only for dlopen
489 immutable runTlsCtors = !_rtLoading;
490 runModuleConstructors(pdso, runTlsCtors);
493 // has backlink => unregister
496 DSO* pdso = cast(DSO*)*data._slot;
499 // don't finalizes modules after rt_term was called (see Bugzilla 11378)
500 if (_isRuntimeInitialized)
502 // rt_unloadLibrary already ran tls dtors, so do this only for dlclose
503 immutable runTlsDtors = !_rtLoading;
504 runModuleDestructors(pdso, runTlsDtors);
505 unregisterGCRanges(pdso);
506 // run finalizers after module dtors (same order as in rt_term)
507 version (Shared) runFinalizers(pdso);
514 /* This DSO was not unloaded by rt_unloadLibrary so we
515 * have to remove it from _loadedDSOs here.
517 foreach (i, ref tdso; _loadedDSOs)
519 if (tdso._pdso == pdso)
521 _loadedDSOs.remove(i);
527 unsetDSOForHandle(pdso, pdso._handle);
531 // static DSOs are unloaded in reverse order
532 safeAssert(pdso == _loadedDSOs.back, "DSO being unregistered isn't current last one.");
533 _loadedDSOs.popBack();
538 // last DSO being unloaded => shutdown registry
539 if (_loadedDSOs.empty)
543 safeAssert(_handleToDSO.empty, "_handleToDSO not in sync with _loadedDSOs.");
544 _handleToDSO.reset();
556 ///////////////////////////////////////////////////////////////////////////////
558 ///////////////////////////////////////////////////////////////////////////////
560 // Shared D libraries are only supported when linking against a shared druntime library.
564 ThreadDSO* findThreadDSO(DSO* pdso) nothrow @nogc
566 foreach (ref tdata; _loadedDSOs)
567 if (tdata._pdso == pdso) return &tdata;
571 void incThreadRef(DSO* pdso, bool incAdd)
573 if (auto tdata = findThreadDSO(pdso)) // already initialized
575 if (incAdd && ++tdata._addCnt > 1) return;
580 foreach (dep; pdso._deps)
581 incThreadRef(dep, false);
582 immutable ushort refCnt = 1, addCnt = incAdd ? 1 : 0;
583 _loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
584 pdso._moduleGroup.runTlsCtors();
588 void decThreadRef(DSO* pdso, bool decAdd)
590 auto tdata = findThreadDSO(pdso);
591 safeAssert(tdata !is null, "Failed to find thread DSO.");
592 safeAssert(!decAdd || tdata._addCnt > 0, "Mismatching rt_unloadLibrary call.");
594 if (decAdd && --tdata._addCnt > 0) return;
595 if (--tdata._refCnt > 0) return;
597 pdso._moduleGroup.runTlsDtors();
598 foreach (i, ref td; _loadedDSOs)
599 if (td._pdso == pdso) _loadedDSOs.remove(i);
600 foreach (dep; pdso._deps)
601 decThreadRef(dep, false);
604 extern(C) void* rt_loadLibrary(const char* name)
606 immutable save = _rtLoading;
608 scope (exit) _rtLoading = save;
610 auto handle = .dlopen(name, RTLD_LAZY);
611 if (handle is null) return null;
613 // if it's a D library
614 if (auto pdso = dsoForHandle(handle))
615 incThreadRef(pdso, true);
619 extern(C) int rt_unloadLibrary(void* handle)
621 if (handle is null) return false;
623 immutable save = _rtLoading;
625 scope (exit) _rtLoading = save;
627 // if it's a D library
628 if (auto pdso = dsoForHandle(handle))
629 decThreadRef(pdso, true);
630 return .dlclose(handle) == 0;
634 ///////////////////////////////////////////////////////////////////////////////
636 ///////////////////////////////////////////////////////////////////////////////
638 void initLocks() nothrow @nogc
641 !pthread_mutex_init(&_handleToDSOMutex, null) || assert(0);
644 void finiLocks() nothrow @nogc
647 !pthread_mutex_destroy(&_handleToDSOMutex) || assert(0);
650 void runModuleConstructors(DSO* pdso, bool runTlsCtors)
652 pdso._moduleGroup.sortCtors();
653 pdso._moduleGroup.runCtors();
654 if (runTlsCtors) pdso._moduleGroup.runTlsCtors();
657 void runModuleDestructors(DSO* pdso, bool runTlsDtors)
659 if (runTlsDtors) pdso._moduleGroup.runTlsDtors();
660 pdso._moduleGroup.runDtors();
663 void registerGCRanges(DSO* pdso) nothrow @nogc
665 foreach (rng; pdso._gcRanges)
666 GC.addRange(rng.ptr, rng.length);
669 void unregisterGCRanges(DSO* pdso) nothrow @nogc
671 foreach (rng; pdso._gcRanges)
672 GC.removeRange(rng.ptr);
675 version (Shared) void runFinalizers(DSO* pdso)
677 foreach (seg; pdso._codeSegments)
678 GC.runFinalizers(seg);
681 void freeDSO(DSO* pdso) nothrow @nogc
683 pdso._gcRanges.reset();
686 pdso._codeSegments.reset();
696 link_map* linkMapForHandle(void* handle)
698 static if (__traits(compiles, RTLD_DI_LINKMAP))
701 const success = dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0;
702 safeAssert(success, "Failed to get DSO info.");
705 else version (OpenBSD)
707 safeAssert(handle !is null, "Failed to get DSO info.");
708 return cast(link_map*)handle;
711 static assert(0, "unimplemented");
714 DSO* dsoForHandle(void* handle)
717 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
718 if (auto ppdso = handle in _handleToDSO)
720 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
724 void setDSOForHandle(DSO* pdso, void* handle)
726 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
727 safeAssert(handle !in _handleToDSO, "DSO already registered.");
728 _handleToDSO[handle] = pdso;
729 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
732 void unsetDSOForHandle(DSO* pdso, void* handle)
734 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
735 safeAssert(_handleToDSO[handle] == pdso, "Handle doesn't match registered DSO.");
736 _handleToDSO.remove(handle);
737 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
740 void getDependencies(const ref dl_phdr_info info, ref Array!(DSO*) deps)
742 // get the entries of the .dynamic section
744 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
746 if (phdr.p_type == PT_DYNAMIC)
748 auto p = cast(ElfW!"Dyn"*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
749 dyns = p[0 .. phdr.p_memsz / ElfW!"Dyn".sizeof];
753 // find the string table which contains the sonames
757 if (dyn.d_tag == DT_STRTAB)
759 version (CRuntime_Musl)
760 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
763 // This might change in future glibc releases (after 2.29) as dynamic sections
764 // are not required to be read-only on RISC-V. This was copy & pasted from MIPS
765 // while upstreaming RISC-V support. Otherwise MIPS is the only arch which sets
766 // in glibc: #define DL_RO_DYN_SECTION 1
768 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
769 else version (MIPS_Any)
770 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
772 strtab = cast(const(char)*)dyn.d_un.d_ptr;
774 else version (FreeBSD)
775 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
776 else version (NetBSD)
777 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
778 else version (OpenBSD)
779 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
780 else version (DragonFlyBSD)
781 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
782 else version (Solaris)
783 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
785 static assert(0, "unimplemented");
791 immutable tag = dyn.d_tag;
792 if (!(tag == DT_NEEDED || tag == DT_AUXILIARY || tag == DT_FILTER))
795 // soname of the dependency
796 auto name = strtab + dyn.d_un.d_val;
797 // get handle without loading the library
798 auto handle = handleForName(name);
799 // the runtime linker has already loaded all dependencies
800 safeAssert(handle !is null, "Failed to get library handle.");
801 // if it's a D library
802 if (auto pdso = dsoForHandle(handle))
803 deps.insertBack(pdso); // append it to the dependencies
807 void* handleForName(const char* name)
809 version (Solaris) enum refCounted = false;
810 else version (OpenBSD) enum refCounted = false;
811 else enum refCounted = true;
813 static if (__traits(compiles, RTLD_NOLOAD))
814 enum flags = (RTLD_NOLOAD | RTLD_LAZY);
816 enum flags = RTLD_LAZY;
818 auto handle = .dlopen(name, flags);
819 static if (refCounted)
822 .dlclose(handle); // drop reference count
828 ///////////////////////////////////////////////////////////////////////////////
829 // Elf program header iteration
830 ///////////////////////////////////////////////////////////////////////////////
833 * Scan segments in Linux dl_phdr_info struct and store
834 * the TLS and writeable data segments in *pdso.
836 void scanSegments(const ref dl_phdr_info info, DSO* pdso) nothrow @nogc
838 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
843 if (phdr.p_flags & PF_W) // writeable data segment
845 auto beg = cast(void*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
846 pdso._gcRanges.insertBack(beg[0 .. phdr.p_memsz]);
848 version (Shared) if (phdr.p_flags & PF_X) // code segment
850 auto beg = cast(void*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
851 pdso._codeSegments.insertBack(beg[0 .. phdr.p_memsz]);
855 case PT_TLS: // TLS segment
861 safeAssert(!pdso._tlsSize, "Multiple TLS segments in image header.");
862 static if (OS_Have_Dlpi_Tls_Modid)
864 pdso._tlsMod = info.dlpi_tls_modid;
865 pdso._tlsSize = phdr.p_memsz;
867 else version (Solaris)
872 const char* rt_pathname;
883 dlinfo(handleForName(info.dlpi_name), RTLD_DI_LINKMAP, &map);
885 dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
886 // Until Solaris 11.4, tlsmodid for the executable is 0.
887 // Let it start at 1 as the rest of the code expects.
888 pdso._tlsMod = map.rt_tlsmodid + 1;
889 pdso._tlsSize = phdr.p_memsz;
905 /**************************
907 * result where the output is to be written; dl_phdr_info is an OS struct
909 * true if found, and *result is filled in
911 * http://linux.die.net/man/3/dl_iterate_phdr
913 bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
915 version (linux) enum IterateManually = true;
916 else version (NetBSD) enum IterateManually = true;
917 else version (OpenBSD) enum IterateManually = true;
918 else version (Solaris) enum IterateManually = true;
919 else enum IterateManually = false;
921 static if (IterateManually)
923 static struct DG { const(void)* addr; dl_phdr_info* result; }
925 extern(C) int callback(dl_phdr_info* info, size_t sz, void* arg) nothrow @nogc
927 auto p = cast(DG*)arg;
928 if (findSegmentForAddr(*info, p.addr))
930 if (p.result !is null) *p.result = *info;
933 return 0; // continue iteration
936 auto dg = DG(addr, result);
938 /* OS function that walks through the list of an application's shared objects and
939 * calls 'callback' once for each object, until either all shared objects
940 * have been processed or 'callback' returns a nonzero value.
942 return dl_iterate_phdr(&callback, &dg) != 0;
944 else version (FreeBSD)
946 return !!_rtld_addr_phdr(addr, result);
948 else version (DragonFlyBSD)
950 return !!_rtld_addr_phdr(addr, result);
953 static assert(0, "unimplemented");
956 /*********************************
957 * Determine if 'addr' lies within shared object 'info'.
958 * If so, return true and fill in 'result' with the corresponding ELF program header.
960 bool findSegmentForAddr(const ref dl_phdr_info info, in void* addr, ElfW!"Phdr"* result=null) nothrow @nogc
962 if (addr < cast(void*)info.dlpi_addr) // less than base address of object means quick reject
965 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
967 auto beg = cast(void*)(info.dlpi_addr + phdr.p_vaddr);
968 if (cast(size_t)(addr - beg) < phdr.p_memsz)
970 if (result !is null) *result = phdr;
977 /**************************
979 * addr an internal address of a DSO
981 * the dlopen handle for that DSO or null if addr is not within a loaded DSO
983 version (Shared) void* handleForAddr(void* addr) nothrow @nogc
986 if (dladdr(addr, &info) != 0)
987 return handleForName(info.dli_fname);
991 ///////////////////////////////////////////////////////////////////////////////
993 ///////////////////////////////////////////////////////////////////////////////
997 * Returns: the TLS memory range for a given module and the calling
998 * thread or null if that module has no TLS.
1000 * Note: This will cause the TLS memory to be eagerly allocated.
1004 version (CRuntime_Glibc)
1006 // For x86_64, fields are of type uint64_t, this is important for x32
1007 // where tls_index would otherwise have the wrong size.
1008 // See https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/dl-tls.h
1027 extern(C) void* __tls_get_addr(tls_index* ti) nothrow @nogc;
1028 extern(C) void* __ibmz_get_tls_offset(tls_index *ti) nothrow @nogc;
1030 /* The dynamic thread vector (DTV) pointers may point 0x8000 past the start of
1031 * each TLS block. This is at least true for PowerPC and Mips platforms.
1032 * See: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/powerpc/dl-tls.h;h=f7cf6f96ebfb505abfd2f02be0ad0e833107c0cd;hb=HEAD#l34
1033 * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mips/dl-tls.h;h=93a6dc050cb144b9f68b96fb3199c60f5b1fcd18;hb=HEAD#l32
1034 * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/riscv/dl-tls.h;h=ab2d860314de94c18812bc894ff6b3f55368f20f;hb=HEAD#l32
1037 enum TLS_DTV_OFFSET = 0x0;
1038 else version (X86_64)
1039 enum TLS_DTV_OFFSET = 0x0;
1041 enum TLS_DTV_OFFSET = 0x0;
1042 else version (AArch64)
1043 enum TLS_DTV_OFFSET = 0x0;
1044 else version (RISCV32)
1045 enum TLS_DTV_OFFSET = 0x800;
1046 else version (RISCV64)
1047 enum TLS_DTV_OFFSET = 0x800;
1049 enum TLS_DTV_OFFSET = 0x0;
1050 else version (SPARC)
1051 enum TLS_DTV_OFFSET = 0x0;
1052 else version (SPARC64)
1053 enum TLS_DTV_OFFSET = 0x0;
1055 enum TLS_DTV_OFFSET = 0x8000;
1056 else version (PPC64)
1057 enum TLS_DTV_OFFSET = 0x8000;
1058 else version (MIPS32)
1059 enum TLS_DTV_OFFSET = 0x8000;
1060 else version (MIPS64)
1061 enum TLS_DTV_OFFSET = 0x8000;
1062 else version (IBMZ_Any)
1063 enum TLS_DTV_OFFSET = 0x0;
1064 else version (LoongArch64)
1065 enum TLS_DTV_OFFSET = 0x0;
1067 static assert( false, "Platform not supported." );
1069 void[] getTLSRange(size_t mod, size_t sz) nothrow @nogc
1074 version (GNU_EMUTLS)
1075 return null; // Handled in scanTLSRanges().
1080 static if (!OS_Have_Dlpi_Tls_Modid)
1085 auto ti = tls_index(mod, 0);
1086 version (CRuntime_Musl)
1087 return (__tls_get_addr(&ti)-TLS_DTV_OFFSET)[0 .. sz];
1088 else version (IBMZ_Any)
1090 // IBM Z only provides __tls_get_offset instead of __tls_get_addr
1091 // which returns an offset relative to the thread pointer.
1092 auto addr = __ibmz_get_tls_offset(&ti);
1093 addr = addr + cast(c_ulong)__builtin_thread_pointer();
1094 return addr[0 .. sz];
1097 return (__tls_get_addr(&ti)-TLS_DTV_OFFSET)[0 .. sz];