1 /**
2  * Written in the D programming language.
3  * This module provides Solaris-specific support for sections.
4  *
5  * OpenBSD (as of 6.9) lacks dlinfo, RTLD_NOLOAD, and dlpt_tls_modid.
6  * Faking support caused problems.
7  * That is why we are not using elf_shared on OpenBSD.
8  *
9  * Copyright: Copyright Martin Nowak 2012-2013.
10  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
11  * Authors:   Martin Nowak
12  * Source: $(DRUNTIMESRC rt/_sections_solaris.d)
13  */
14 
15 module rt.sections_solaris;
16 
17 version (Solaris)
18 {
19     version = SolarisOrOpenBSD;
20 }
21 else version (OpenBSD)
22 {
23     version = SolarisOrOpenBSD;
24 }
25 
26 version (SolarisOrOpenBSD):
27 
28 // debug = PRINTF;
29 debug(PRINTF) import core.stdc.stdio;
30 import core.stdc.stdlib : malloc, free;
31 import rt.deh, rt.minfo;
32 
33 struct SectionGroup
34 {
35     static int opApply(scope int delegate(ref SectionGroup) dg)
36     {
37         return dg(_sections);
38     }
39 
40     static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
41     {
42         return dg(_sections);
43     }
44 
45     @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
46     {
47         return _moduleGroup.modules;
48     }
49 
50     @property ref inout(ModuleGroup) moduleGroup() inout return nothrow @nogc
51     {
52         return _moduleGroup;
53     }
54 
55     @property immutable(FuncTable)[] ehTables() const nothrow @nogc
56     {
57         auto pbeg = cast(immutable(FuncTable)*)&__start_deh;
58         auto pend = cast(immutable(FuncTable)*)&__stop_deh;
59         return pbeg[0 .. pend - pbeg];
60     }
61 
62     @property inout(void[])[] gcRanges() inout return nothrow @nogc
63     {
64         return _gcRanges[];
65     }
66 
67 private:
68     ModuleGroup _moduleGroup;
69     void[][1] _gcRanges;
70 }
71 
72 void initSections() nothrow @nogc
73 {
74     auto mbeg = cast(immutable ModuleInfo**)&__start_minfo;
75     auto mend = cast(immutable ModuleInfo**)&__stop_minfo;
76     _sections.moduleGroup = ModuleGroup(mbeg[0 .. mend - mbeg]);
77 
78     auto pbeg = cast(void*)&__dso_handle;
79     auto pend = cast(void*)&_end;
80     _sections._gcRanges[0] = pbeg[0 .. pend - pbeg];
81 }
82 
83 void finiSections() nothrow @nogc
84 {
85 }
86 
87 void[] initTLSRanges() nothrow @nogc
88 {
89     auto pbeg = cast(void*)&_tlsstart;
90     auto pend = cast(void*)&_tlsend;
91     return pbeg[0 .. pend - pbeg];
92 }
93 
94 void finiTLSRanges(void[] rng) nothrow @nogc
95 {
96 }
97 
98 void scanTLSRanges(void[] rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
99 {
100     dg(rng.ptr, rng.ptr + rng.length);
101 }
102 
103 private:
104 
105 __gshared SectionGroup _sections;
106 
107 extern(C)
108 {
109     /* Symbols created by the compiler/linker and inserted into the
110      * object file that 'bracket' sections.
111      */
112     extern __gshared
113     {
114         void* __start_deh;
115         void* __stop_deh;
116         void* __start_minfo;
117         void* __stop_minfo;
118         int __dso_handle;
119         int _end;
120     }
121 
122     extern
123     {
124         void* _tlsstart;
125         void* _tlsend;
126     }
127 }