xref: /netbsd-src/external/gpl3/gcc.old/dist/libphobos/libdruntime/gcc/sections/win32.d (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1 // Win32-specific support for sections.
2 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 
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
7 // version.
8 
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
12 // for more details.
13 
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.
17 
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/>.
22 
23 module gcc.sections.win32;
24 
25 version (CRuntime_DigitalMars):
26 
27 // debug = PRINTF;
28 debug(PRINTF) import core.stdc.stdio;
29 import rt.minfo;
30 import core.stdc.stdlib : malloc, free;
31 
32 struct SectionGroup
33 {
opApplySectionGroup34     static int opApply(scope int delegate(ref SectionGroup) dg)
35     {
36         return dg(_sections);
37     }
38 
opApplyReverseSectionGroup39     static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
40     {
41         return dg(_sections);
42     }
43 
immutableSectionGroup44     @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
45     {
46         return _moduleGroup.modules;
47     }
48 
inoutSectionGroup49     @property ref inout(ModuleGroup) moduleGroup() inout nothrow @nogc
50     {
51         return _moduleGroup;
52     }
53 
inoutSectionGroup54     @property inout(void[])[] gcRanges() inout nothrow @nogc
55     {
56         return _gcRanges[];
57     }
58 
59 private:
60     ModuleGroup _moduleGroup;
61     void[][] _gcRanges;
62 }
63 
64 shared(bool) conservative;
65 
initSections()66 void initSections() nothrow @nogc
67 {
68     _sections._moduleGroup = ModuleGroup(getModuleInfos());
69 
70     import rt.sections;
71     conservative = !scanDataSegPrecisely();
72 
73     if (conservative)
74     {
75         _sections._gcRanges = (cast(void[]*) malloc(2 * (void[]).sizeof))[0..2];
76 
77         auto databeg = cast(void*)&_xi_a;
78         auto dataend = cast(void*)_moduleinfo_array.ptr;
79         _sections._gcRanges[0] = databeg[0 .. dataend - databeg];
80 
81         // skip module info and CONST segment
82         auto bssbeg = cast(void*)&_edata;
83         auto bssend = cast(void*)&_end;
84         _sections._gcRanges[1] = bssbeg[0 .. bssend - bssbeg];
85     }
86     else
87     {
88         size_t count = &_DPend - &_DPbegin;
89         auto ranges = cast(void[]*) malloc(count * (void[]).sizeof);
90         size_t r = 0;
91         void* prev = null;
92         for (size_t i = 0; i < count; i++)
93         {
94             void* addr = (&_DPbegin)[i];
95             if (prev + (void*).sizeof == addr)
96                 ranges[r-1] = ranges[r-1].ptr[0 .. ranges[r-1].length + (void*).sizeof];
97             else
98                 ranges[r++] = (cast(void**)addr)[0..1];
99             prev = addr;
100         }
101         _sections._gcRanges = ranges[0..r];
102     }
103 }
104 
finiSections()105 void finiSections() nothrow @nogc
106 {
107     free(_sections._gcRanges.ptr);
108 }
109 
initTLSRanges()110 void[] initTLSRanges() nothrow @nogc
111 {
112     auto pbeg = cast(void*)&_tlsstart;
113     auto pend = cast(void*)&_tlsend;
114     return pbeg[0 .. pend - pbeg];
115 }
116 
finiTLSRanges(void[]rng)117 void finiTLSRanges(void[] rng) nothrow @nogc
118 {
119 }
120 
scanTLSRanges(void[]rng,scope void delegate (void * pbeg,void * pend)nothrow dg)121 void scanTLSRanges(void[] rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
122 {
123     if (conservative)
124     {
125         dg(rng.ptr, rng.ptr + rng.length);
126     }
127     else
128     {
129         for (auto p = &_TPbegin; p < &_TPend; )
130         {
131             uint beg = *p++;
132             uint end = beg + cast(uint)((void*).sizeof);
133             while (p < &_TPend && *p == end)
134             {
135                 end += (void*).sizeof;
136                 p++;
137             }
138             dg(rng.ptr + beg, rng.ptr + end);
139         }
140     }
141 }
142 
143 private:
144 
145 __gshared SectionGroup _sections;
146 
147 // Windows: this gets initialized by minit.asm
148 extern(C) __gshared immutable(ModuleInfo*)[] _moduleinfo_array;
149 extern(C) void _minit() nothrow @nogc;
150 
immutable(ModuleInfo *)151 immutable(ModuleInfo*)[] getModuleInfos() nothrow @nogc
152 out (result)
153 {
154     foreach (m; result)
155         assert(m !is null);
156 }
157 body
158 {
159     // _minit directly alters the global _moduleinfo_array
160     _minit();
161     return _moduleinfo_array;
162 }
163 
164 extern(C)
165 {
166     extern __gshared
167     {
168         int _xi_a;      // &_xi_a just happens to be start of data segment
169         int _edata;     // &_edata is start of BSS segment
170         int _end;       // &_end is past end of BSS
171 
172         void* _DPbegin; // first entry in the array of pointers addresses
173         void* _DPend;   // &_DPend points after last entry of array
174         uint _TPbegin;  // first entry in the array of TLS offsets of pointers
175         uint _TPend;    // &_DPend points after last entry of array
176     }
177 
178     extern
179     {
180         int _tlsstart;
181         int _tlsend;
182     }
183 }
184