1 // ELF-specific support for sections with shared libraries.
2 // Copyright (C) 2019-2021 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_shared;
25 version (RISCV32) version = RISCV_Any;
26 version (RISCV64) version = RISCV_Any;
27 version (S390) version = IBMZ_Any;
28 version (SystemZ) version = IBMZ_Any;
30 version (CRuntime_Glibc) enum SharedELF = true;
31 else version (CRuntime_Musl) enum SharedELF = true;
32 else version (FreeBSD) enum SharedELF = true;
33 else version (NetBSD) enum SharedELF = true;
34 else version (DragonFlyBSD) enum SharedELF = true;
35 else version (CRuntime_UClibc) enum SharedELF = true;
36 else version (Solaris) enum SharedELF = true;
37 else enum SharedELF = false;
38 static if (SharedELF):
42 import core.stdc.config;
43 import core.stdc.stdio;
44 import core.stdc.stdlib : calloc, exit, free, malloc, EXIT_FAILURE;
45 import core.stdc.string : strlen;
48 import core.sys.linux.dlfcn;
49 import core.sys.linux.elf;
50 import core.sys.linux.link;
52 else version (FreeBSD)
54 import core.sys.freebsd.dlfcn;
55 import core.sys.freebsd.sys.elf;
56 import core.sys.freebsd.sys.link_elf;
60 import core.sys.netbsd.dlfcn;
61 import core.sys.netbsd.sys.elf;
62 import core.sys.netbsd.sys.link_elf;
64 else version (DragonFlyBSD)
66 import core.sys.dragonflybsd.dlfcn;
67 import core.sys.dragonflybsd.sys.elf;
68 import core.sys.dragonflybsd.sys.link_elf;
70 else version (Solaris)
72 import core.sys.solaris.dlfcn;
73 import core.sys.solaris.link;
74 import core.sys.solaris.sys.elf;
75 import core.sys.solaris.sys.link;
79 static assert(0, "unimplemented");
81 import core.sys.posix.pthread;
87 import rt.util.container.array;
88 import rt.util.container.hashtab;
91 * Asserts the specified condition, independent from -release, by abort()ing.
92 * Regular assertions throw an AssertError and thus require an initialized
93 * GC, which isn't the case (yet or anymore) for the startup/shutdown code in
94 * this module (called by CRT ctors/dtors etc.).
96 private void safeAssert(bool condition, scope string msg, size_t line = __LINE__) @nogc nothrow @safe
98 import core.internal.abort;
99 condition || abort(msg, __FILE__, line);
102 alias DSO SectionGroup;
105 static int opApply(scope int delegate(ref DSO) dg)
107 foreach (dso; _loadedDSOs)
109 if (auto res = dg(*dso))
115 static int opApplyReverse(scope int delegate(ref DSO) dg)
117 foreach_reverse (dso; _loadedDSOs)
119 if (auto res = dg(*dso))
125 @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
127 return _moduleGroup.modules;
130 @property ref inout(ModuleGroup) moduleGroup() inout nothrow @nogc
135 @property immutable(FuncTable)[] ehTables() const nothrow @nogc
140 @property inout(void[])[] gcRanges() inout nothrow @nogc
149 safeAssert(_moduleGroup.modules.length > 0, "No modules for DSO.");
150 safeAssert(_tlsMod || !_tlsSize, "Inconsistent TLS fields for DSO.");
153 ModuleGroup _moduleGroup;
154 Array!(void[]) _gcRanges;
160 Array!(void[]) _codeSegments; // array of code segments
161 Array!(DSO*) _deps; // D libraries needed by this DSO
162 void* _handle; // corresponding handle
165 // get the TLS range for the executing thread
166 void[] tlsRange() const nothrow @nogc
168 return getTLSRange(_tlsMod, _tlsSize);
173 * Boolean flag set to true while the runtime is initialized.
175 __gshared bool _isRuntimeInitialized;
178 version (FreeBSD) private __gshared void* dummy_ref;
179 version (DragonFlyBSD) private __gshared void* dummy_ref;
180 version (NetBSD) private __gshared void* dummy_ref;
181 version (Solaris) private __gshared void* dummy_ref;
184 * Gets called on program startup just before GC is initialized.
186 void initSections() nothrow @nogc
188 _isRuntimeInitialized = true;
189 // reference symbol to support weak linkage
190 version (FreeBSD) dummy_ref = &_d_dso_registry;
191 version (DragonFlyBSD) dummy_ref = &_d_dso_registry;
192 version (NetBSD) dummy_ref = &_d_dso_registry;
193 version (Solaris) dummy_ref = &_d_dso_registry;
198 * Gets called on program shutdown just after GC is terminated.
200 void finiSections() nothrow @nogc
202 _isRuntimeInitialized = false;
205 alias ScanDG = void delegate(void* pbeg, void* pend) nothrow;
210 * Called once per thread; returns array of thread local storage ranges
212 Array!(ThreadDSO)* initTLSRanges() @nogc nothrow
214 return &_loadedDSOs();
217 void finiTLSRanges(Array!(ThreadDSO)* tdsos) @nogc nothrow
219 // Nothing to do here. tdsos used to point to the _loadedDSOs instance
220 // in the dying thread's TLS segment and as such is not valid anymore.
221 // The memory for the array contents was already reclaimed in
222 // cleanupLoadedLibraries().
225 void scanTLSRanges(Array!(ThreadDSO)* tdsos, scope ScanDG dg) nothrow
234 foreach (ref tdso; *tdsos)
235 dg(tdso._tlsRange.ptr, tdso._tlsRange.ptr + tdso._tlsRange.length);
239 // interface for core.thread to inherit loaded libraries
240 void* pinLoadedLibraries() nothrow @nogc
242 auto res = cast(Array!(ThreadDSO)*)calloc(1, Array!(ThreadDSO).sizeof);
243 res.length = _loadedDSOs.length;
244 foreach (i, ref tdso; _loadedDSOs)
249 // Increment the dlopen ref for explicitly loaded libraries to pin them.
250 const success = .dlopen(linkMapForHandle(tdso._pdso._handle).l_name, RTLD_LAZY) !is null;
251 safeAssert(success, "Failed to increment dlopen ref.");
252 (*res)[i]._addCnt = 1; // new array takes over the additional ref count
258 void unpinLoadedLibraries(void* p) nothrow @nogc
260 auto pary = cast(Array!(ThreadDSO)*)p;
261 // In case something failed we need to undo the pinning.
262 foreach (ref tdso; *pary)
266 auto handle = tdso._pdso._handle;
267 safeAssert(handle !is null, "Invalid library handle.");
275 // Called before TLS ctors are ran, copy over the loaded libraries
276 // of the parent thread.
277 void inheritLoadedLibraries(void* p) nothrow @nogc
279 safeAssert(_loadedDSOs.empty, "DSOs have already been registered for this thread.");
280 _loadedDSOs.swap(*cast(Array!(ThreadDSO)*)p);
282 foreach (ref dso; _loadedDSOs)
284 // the copied _tlsRange corresponds to parent thread
285 dso.updateTLSRange();
289 // Called after all TLS dtors ran, decrements all remaining dlopen refs.
290 void cleanupLoadedLibraries() nothrow @nogc
292 foreach (ref tdso; _loadedDSOs)
294 if (tdso._addCnt == 0) continue;
296 auto handle = tdso._pdso._handle;
297 safeAssert(handle !is null, "Invalid DSO handle.");
298 for (; tdso._addCnt > 0; --tdso._addCnt)
302 // Free the memory for the array contents.
309 * Called once per thread; returns array of thread local storage ranges
311 Array!(void[])* initTLSRanges() nothrow @nogc
313 auto rngs = &_tlsRanges();
316 foreach (ref pdso; _loadedDSOs)
317 rngs.insertBack(pdso.tlsRange());
322 void finiTLSRanges(Array!(void[])* rngs) nothrow @nogc
327 void scanTLSRanges(Array!(void[])* rngs, scope ScanDG dg) nothrow
337 dg(rng.ptr, rng.ptr + rng.length);
347 * Array of thread local DSO metadata for all libraries loaded and
348 * initialized in this thread.
351 * A newly spawned thread will inherit these libraries.
353 * We use an array here to preserve the order of
354 * initialization. If that became a performance issue, we
355 * could use a hash table and enumerate the DSOs during
356 * loading so that the hash table values could be sorted when
362 static if (_pdso.sizeof == 8) uint _refCnt, _addCnt;
363 else static if (_pdso.sizeof == 4) ushort _refCnt, _addCnt;
364 else static assert(0, "unimplemented");
367 // update the _tlsRange for the executing thread
368 void updateTLSRange() nothrow @nogc
370 _tlsRange = _pdso.tlsRange();
373 @property ref Array!(ThreadDSO) _loadedDSOs() @nogc nothrow { static Array!(ThreadDSO) x; return x; }
376 * Set to true during rt_loadLibrary/rt_unloadLibrary calls.
381 * Hash table to map link_map* to corresponding DSO*.
382 * The hash table is protected by a Mutex.
384 __gshared pthread_mutex_t _handleToDSOMutex;
385 @property ref HashTab!(void*, DSO*) _handleToDSO() @nogc nothrow { __gshared HashTab!(void*, DSO*) x; return x; }
388 * Section in executable that contains copy relocations.
389 * Might be null when druntime is dynamically loaded by a C host.
391 __gshared const(void)[] _copyRelocSection;
396 * Static DSOs loaded by the runtime linker. This includes the
397 * executable. These can't be unloaded.
399 @property ref Array!(DSO*) _loadedDSOs() @nogc nothrow { __gshared Array!(DSO*) x; return x; }
402 * Thread local array that contains TLS memory ranges for each
403 * library initialized in this thread.
405 @property ref Array!(void[]) _tlsRanges() @nogc nothrow { static Array!(void[]) x; return x; }
407 enum _rtLoading = false;
410 ///////////////////////////////////////////////////////////////////////////////
411 // Compiler to runtime interface.
412 ///////////////////////////////////////////////////////////////////////////////
415 * This data structure is generated by the compiler, and then passed to
418 struct CompilerDSOData
420 size_t _version; // currently 1
421 void** _slot; // can be used to store runtime data
422 immutable(object.ModuleInfo*)* _minfo_beg, _minfo_end; // array of modules in this object file
425 T[] toRange(T)(T* beg, T* end) { return beg[0 .. end - beg]; }
427 /* For each shared library and executable, the compiler generates code that
428 * sets up CompilerDSOData and calls _d_dso_registry().
429 * A pointer to that code is inserted into both the .ctors and .dtors
430 * segment so it gets called by the loader on startup and shutdown.
432 extern(C) void _d_dso_registry(CompilerDSOData* data)
434 // only one supported currently
435 safeAssert(data._version >= 1, "Incompatible compiler-generated DSO data version.");
437 // no backlink => register
438 if (*data._slot is null)
440 immutable firstDSO = _loadedDSOs.empty;
441 if (firstDSO) initLocks();
443 DSO* pdso = cast(DSO*).calloc(1, DSO.sizeof);
444 assert(typeid(DSO).initializer().ptr is null);
445 *data._slot = pdso; // store backlink in library record
447 pdso._moduleGroup = ModuleGroup(toRange(data._minfo_beg, data._minfo_end));
449 dl_phdr_info info = void;
450 const headerFound = findDSOInfoForAddr(data._slot, &info);
451 safeAssert(headerFound, "Failed to find image header.");
453 scanSegments(info, pdso);
457 auto handle = handleForAddr(data._slot);
459 getDependencies(info, pdso._deps);
460 pdso._handle = handle;
461 setDSOForHandle(pdso, pdso._handle);
465 /* This DSO was not loaded by rt_loadLibrary which
466 * happens for all dependencies of an executable or
467 * the first dlopen call from a C program.
468 * In this case we add the DSO to the _loadedDSOs of this
469 * thread with a refCnt of 1 and call the TlsCtors.
471 immutable ushort refCnt = 1, addCnt = 0;
472 _loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
477 foreach (p; _loadedDSOs)
478 safeAssert(p !is pdso, "DSO already registered.");
479 _loadedDSOs.insertBack(pdso);
480 _tlsRanges.insertBack(pdso.tlsRange());
483 // don't initialize modules before rt_init was called (see Bugzilla 11378)
484 if (_isRuntimeInitialized)
486 registerGCRanges(pdso);
487 // rt_loadLibrary will run tls ctors, so do this only for dlopen
488 immutable runTlsCtors = !_rtLoading;
489 runModuleConstructors(pdso, runTlsCtors);
492 // has backlink => unregister
495 DSO* pdso = cast(DSO*)*data._slot;
498 // don't finalizes modules after rt_term was called (see Bugzilla 11378)
499 if (_isRuntimeInitialized)
501 // rt_unloadLibrary already ran tls dtors, so do this only for dlclose
502 immutable runTlsDtors = !_rtLoading;
503 runModuleDestructors(pdso, runTlsDtors);
504 unregisterGCRanges(pdso);
505 // run finalizers after module dtors (same order as in rt_term)
506 version (Shared) runFinalizers(pdso);
513 /* This DSO was not unloaded by rt_unloadLibrary so we
514 * have to remove it from _loadedDSOs here.
516 foreach (i, ref tdso; _loadedDSOs)
518 if (tdso._pdso == pdso)
520 _loadedDSOs.remove(i);
526 unsetDSOForHandle(pdso, pdso._handle);
530 // static DSOs are unloaded in reverse order
531 safeAssert(pdso == _loadedDSOs.back, "DSO being unregistered isn't current last one.");
532 _loadedDSOs.popBack();
537 // last DSO being unloaded => shutdown registry
538 if (_loadedDSOs.empty)
542 safeAssert(_handleToDSO.empty, "_handleToDSO not in sync with _loadedDSOs.");
543 _handleToDSO.reset();
555 ///////////////////////////////////////////////////////////////////////////////
557 ///////////////////////////////////////////////////////////////////////////////
559 // Shared D libraries are only supported when linking against a shared druntime library.
563 ThreadDSO* findThreadDSO(DSO* pdso) nothrow @nogc
565 foreach (ref tdata; _loadedDSOs)
566 if (tdata._pdso == pdso) return &tdata;
570 void incThreadRef(DSO* pdso, bool incAdd)
572 if (auto tdata = findThreadDSO(pdso)) // already initialized
574 if (incAdd && ++tdata._addCnt > 1) return;
579 foreach (dep; pdso._deps)
580 incThreadRef(dep, false);
581 immutable ushort refCnt = 1, addCnt = incAdd ? 1 : 0;
582 _loadedDSOs.insertBack(ThreadDSO(pdso, refCnt, addCnt, pdso.tlsRange()));
583 pdso._moduleGroup.runTlsCtors();
587 void decThreadRef(DSO* pdso, bool decAdd)
589 auto tdata = findThreadDSO(pdso);
590 safeAssert(tdata !is null, "Failed to find thread DSO.");
591 safeAssert(!decAdd || tdata._addCnt > 0, "Mismatching rt_unloadLibrary call.");
593 if (decAdd && --tdata._addCnt > 0) return;
594 if (--tdata._refCnt > 0) return;
596 pdso._moduleGroup.runTlsDtors();
597 foreach (i, ref td; _loadedDSOs)
598 if (td._pdso == pdso) _loadedDSOs.remove(i);
599 foreach (dep; pdso._deps)
600 decThreadRef(dep, false);
603 extern(C) void* rt_loadLibrary(const char* name)
605 immutable save = _rtLoading;
607 scope (exit) _rtLoading = save;
609 auto handle = .dlopen(name, RTLD_LAZY);
610 if (handle is null) return null;
612 // if it's a D library
613 if (auto pdso = dsoForHandle(handle))
614 incThreadRef(pdso, true);
618 extern(C) int rt_unloadLibrary(void* handle)
620 if (handle is null) return false;
622 immutable save = _rtLoading;
624 scope (exit) _rtLoading = save;
626 // if it's a D library
627 if (auto pdso = dsoForHandle(handle))
628 decThreadRef(pdso, true);
629 return .dlclose(handle) == 0;
633 ///////////////////////////////////////////////////////////////////////////////
635 ///////////////////////////////////////////////////////////////////////////////
637 void initLocks() nothrow @nogc
640 !pthread_mutex_init(&_handleToDSOMutex, null) || assert(0);
643 void finiLocks() nothrow @nogc
646 !pthread_mutex_destroy(&_handleToDSOMutex) || assert(0);
649 void runModuleConstructors(DSO* pdso, bool runTlsCtors)
651 pdso._moduleGroup.sortCtors();
652 pdso._moduleGroup.runCtors();
653 if (runTlsCtors) pdso._moduleGroup.runTlsCtors();
656 void runModuleDestructors(DSO* pdso, bool runTlsDtors)
658 if (runTlsDtors) pdso._moduleGroup.runTlsDtors();
659 pdso._moduleGroup.runDtors();
662 void registerGCRanges(DSO* pdso) nothrow @nogc
664 foreach (rng; pdso._gcRanges)
665 GC.addRange(rng.ptr, rng.length);
668 void unregisterGCRanges(DSO* pdso) nothrow @nogc
670 foreach (rng; pdso._gcRanges)
671 GC.removeRange(rng.ptr);
674 version (Shared) void runFinalizers(DSO* pdso)
676 foreach (seg; pdso._codeSegments)
677 GC.runFinalizers(seg);
680 void freeDSO(DSO* pdso) nothrow @nogc
682 pdso._gcRanges.reset();
685 pdso._codeSegments.reset();
695 link_map* linkMapForHandle(void* handle)
698 const success = dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0;
699 safeAssert(success, "Failed to get DSO info.");
703 link_map* exeLinkMap(link_map* map)
705 safeAssert(map !is null, "Invalid link_map.");
706 while (map.l_prev !is null)
711 DSO* dsoForHandle(void* handle)
714 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
715 if (auto ppdso = handle in _handleToDSO)
717 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
721 void setDSOForHandle(DSO* pdso, void* handle)
723 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
724 safeAssert(handle !in _handleToDSO, "DSO already registered.");
725 _handleToDSO[handle] = pdso;
726 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
729 void unsetDSOForHandle(DSO* pdso, void* handle)
731 !pthread_mutex_lock(&_handleToDSOMutex) || assert(0);
732 safeAssert(_handleToDSO[handle] == pdso, "Handle doesn't match registered DSO.");
733 _handleToDSO.remove(handle);
734 !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0);
737 void getDependencies(in ref dl_phdr_info info, ref Array!(DSO*) deps)
739 // get the entries of the .dynamic section
741 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
743 if (phdr.p_type == PT_DYNAMIC)
745 auto p = cast(ElfW!"Dyn"*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
746 dyns = p[0 .. phdr.p_memsz / ElfW!"Dyn".sizeof];
750 // find the string table which contains the sonames
754 if (dyn.d_tag == DT_STRTAB)
756 version (CRuntime_Musl)
757 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
760 // This might change in future glibc releases (after 2.29) as dynamic sections
761 // are not required to be read-only on RISC-V. This was copy & pasted from MIPS
762 // while upstreaming RISC-V support. Otherwise MIPS is the only arch which sets
763 // in glibc: #define DL_RO_DYN_SECTION 1
765 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
767 strtab = cast(const(char)*)dyn.d_un.d_ptr;
769 else version (FreeBSD)
770 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
771 else version (NetBSD)
772 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
773 else version (DragonFlyBSD)
774 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
775 else version (Solaris)
776 strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
778 static assert(0, "unimplemented");
784 immutable tag = dyn.d_tag;
785 if (!(tag == DT_NEEDED || tag == DT_AUXILIARY || tag == DT_FILTER))
788 // soname of the dependency
789 auto name = strtab + dyn.d_un.d_val;
790 // get handle without loading the library
791 auto handle = handleForName(name);
792 // the runtime linker has already loaded all dependencies
793 safeAssert(handle !is null, "Failed to get library handle.");
794 // if it's a D library
795 if (auto pdso = dsoForHandle(handle))
796 deps.insertBack(pdso); // append it to the dependencies
800 void* handleForName(const char* name)
802 auto handle = .dlopen(name, RTLD_NOLOAD | RTLD_LAZY);
803 version (Solaris) { }
804 else if (handle !is null) .dlclose(handle); // drop reference count
809 ///////////////////////////////////////////////////////////////////////////////
810 // Elf program header iteration
811 ///////////////////////////////////////////////////////////////////////////////
814 * Scan segments in Linux dl_phdr_info struct and store
815 * the TLS and writeable data segments in *pdso.
817 void scanSegments(in ref dl_phdr_info info, DSO* pdso) nothrow @nogc
819 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
824 if (phdr.p_flags & PF_W) // writeable data segment
826 auto beg = cast(void*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
827 pdso._gcRanges.insertBack(beg[0 .. phdr.p_memsz]);
829 version (Shared) if (phdr.p_flags & PF_X) // code segment
831 auto beg = cast(void*)(info.dlpi_addr + (phdr.p_vaddr & ~(size_t.sizeof - 1)));
832 pdso._codeSegments.insertBack(beg[0 .. phdr.p_memsz]);
836 case PT_TLS: // TLS segment
842 safeAssert(!pdso._tlsSize, "Multiple TLS segments in image header.");
843 static if (OS_Have_Dlpi_Tls_Modid)
845 pdso._tlsMod = info.dlpi_tls_modid;
846 pdso._tlsSize = phdr.p_memsz;
848 else version (Solaris)
853 const char* rt_pathname;
864 dlinfo(handleForName(info.dlpi_name), RTLD_DI_LINKMAP, &map);
866 dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
867 // Until Solaris 11.4, tlsmodid for the executable is 0.
868 // Let it start at 1 as the rest of the code expects.
869 pdso._tlsMod = map.rt_tlsmodid + 1;
870 pdso._tlsSize = phdr.p_memsz;
886 /**************************
888 * result where the output is to be written; dl_phdr_info is an OS struct
890 * true if found, and *result is filled in
892 * http://linux.die.net/man/3/dl_iterate_phdr
894 bool findDSOInfoForAddr(in void* addr, dl_phdr_info* result=null) nothrow @nogc
896 version (linux) enum IterateManually = true;
897 else version (NetBSD) enum IterateManually = true;
898 else version (Solaris) enum IterateManually = true;
899 else enum IterateManually = false;
901 static if (IterateManually)
903 static struct DG { const(void)* addr; dl_phdr_info* result; }
905 extern(C) int callback(dl_phdr_info* info, size_t sz, void* arg) nothrow @nogc
907 auto p = cast(DG*)arg;
908 if (findSegmentForAddr(*info, p.addr))
910 if (p.result !is null) *p.result = *info;
913 return 0; // continue iteration
916 auto dg = DG(addr, result);
918 /* OS function that walks through the list of an application's shared objects and
919 * calls 'callback' once for each object, until either all shared objects
920 * have been processed or 'callback' returns a nonzero value.
922 return dl_iterate_phdr(&callback, &dg) != 0;
924 else version (FreeBSD)
926 return !!_rtld_addr_phdr(addr, result);
928 else version (DragonFlyBSD)
930 return !!_rtld_addr_phdr(addr, result);
933 static assert(0, "unimplemented");
936 /*********************************
937 * Determine if 'addr' lies within shared object 'info'.
938 * If so, return true and fill in 'result' with the corresponding ELF program header.
940 bool findSegmentForAddr(in ref dl_phdr_info info, in void* addr, ElfW!"Phdr"* result=null) nothrow @nogc
942 if (addr < cast(void*)info.dlpi_addr) // less than base address of object means quick reject
945 foreach (ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
947 auto beg = cast(void*)(info.dlpi_addr + phdr.p_vaddr);
948 if (cast(size_t)(addr - beg) < phdr.p_memsz)
950 if (result !is null) *result = phdr;
957 version (linux) import core.sys.linux.errno : program_invocation_name;
958 // should be in core.sys.freebsd.stdlib
959 version (FreeBSD) extern(C) const(char)* getprogname() nothrow @nogc;
960 version (DragonFlyBSD) extern(C) const(char)* getprogname() nothrow @nogc;
961 version (NetBSD) extern(C) const(char)* getprogname() nothrow @nogc;
962 version (Solaris) extern(C) const(char)* getprogname() nothrow @nogc;
964 @property const(char)* progname() nothrow @nogc
966 version (linux) return program_invocation_name;
967 version (FreeBSD) return getprogname();
968 version (DragonFlyBSD) return getprogname();
969 version (NetBSD) return getprogname();
970 version (Solaris) return getprogname();
973 const(char)[] dsoName(const char* dlpi_name) nothrow @nogc
975 // the main executable doesn't have a name in its dlpi_name field
976 const char* p = dlpi_name[0] != 0 ? dlpi_name : progname;
977 return p[0 .. strlen(p)];
980 /**************************
982 * addr an internal address of a DSO
984 * the dlopen handle for that DSO or null if addr is not within a loaded DSO
986 version (Shared) void* handleForAddr(void* addr) nothrow @nogc
989 if (dladdr(addr, &info) != 0)
990 return handleForName(info.dli_fname);
994 ///////////////////////////////////////////////////////////////////////////////
996 ///////////////////////////////////////////////////////////////////////////////
1000 * Returns: the TLS memory range for a given module and the calling
1001 * thread or null if that module has no TLS.
1003 * Note: This will cause the TLS memory to be eagerly allocated.
1007 version (CRuntime_Glibc)
1009 // For x86_64, fields are of type uint64_t, this is important for x32
1010 // where tls_index would otherwise have the wrong size.
1011 // See https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/dl-tls.h
1030 extern(C) void* __tls_get_addr(tls_index* ti) nothrow @nogc;
1031 extern(C) void* __ibmz_get_tls_offset(tls_index *ti) nothrow @nogc;
1033 /* The dynamic thread vector (DTV) pointers may point 0x8000 past the start of
1034 * each TLS block. This is at least true for PowerPC and Mips platforms.
1035 * See: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/powerpc/dl-tls.h;h=f7cf6f96ebfb505abfd2f02be0ad0e833107c0cd;hb=HEAD#l34
1036 * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mips/dl-tls.h;h=93a6dc050cb144b9f68b96fb3199c60f5b1fcd18;hb=HEAD#l32
1037 * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/riscv/dl-tls.h;h=ab2d860314de94c18812bc894ff6b3f55368f20f;hb=HEAD#l32
1040 enum TLS_DTV_OFFSET = 0x0;
1041 else version (X86_64)
1042 enum TLS_DTV_OFFSET = 0x0;
1044 enum TLS_DTV_OFFSET = 0x0;
1045 else version (AArch64)
1046 enum TLS_DTV_OFFSET = 0x0;
1047 else version (RISCV32)
1048 enum TLS_DTV_OFFSET = 0x800;
1049 else version (RISCV64)
1050 enum TLS_DTV_OFFSET = 0x800;
1052 enum TLS_DTV_OFFSET = 0x0;
1053 else version (SPARC)
1054 enum TLS_DTV_OFFSET = 0x0;
1055 else version (SPARC64)
1056 enum TLS_DTV_OFFSET = 0x0;
1058 enum TLS_DTV_OFFSET = 0x8000;
1059 else version (PPC64)
1060 enum TLS_DTV_OFFSET = 0x8000;
1061 else version (MIPS32)
1062 enum TLS_DTV_OFFSET = 0x8000;
1063 else version (MIPS64)
1064 enum TLS_DTV_OFFSET = 0x8000;
1065 else version (IBMZ_Any)
1066 enum TLS_DTV_OFFSET = 0x0;
1068 static assert( false, "Platform not supported." );
1070 void[] getTLSRange(size_t mod, size_t sz) nothrow @nogc
1075 version (GNU_EMUTLS)
1076 return null; // Handled in scanTLSRanges().
1081 static if (!OS_Have_Dlpi_Tls_Modid)
1086 auto ti = tls_index(mod, 0);
1087 version (CRuntime_Musl)
1088 return (__tls_get_addr(&ti)-TLS_DTV_OFFSET)[0 .. sz];
1089 else version (IBMZ_Any)
1091 // IBM Z only provides __tls_get_offset instead of __tls_get_addr
1092 // which returns an offset relative to the thread pointer.
1093 auto addr = __ibmz_get_tls_offset(&ti);
1094 addr = addr + cast(c_ulong)__builtin_thread_pointer();
1095 return addr[0 .. sz];
1098 return (__tls_get_addr(&ti)-TLS_DTV_OFFSET)[0 .. sz];