1 // Win32-specific support for sections.
2 // Copyright (C) 2019 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.win32;
25 version (CRuntime_DigitalMars):
28 debug(PRINTF) import core.stdc.stdio;
30 import core.stdc.stdlib : malloc, free;
34 static int opApply(scope int delegate(ref SectionGroup) dg)
39 static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
44 @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
46 return _moduleGroup.modules;
49 @property ref inout(ModuleGroup) moduleGroup() inout nothrow @nogc
54 @property inout(void[])[] gcRanges() inout nothrow @nogc
60 ModuleGroup _moduleGroup;
64 shared(bool) conservative;
66 void initSections() nothrow @nogc
68 _sections._moduleGroup = ModuleGroup(getModuleInfos());
71 conservative = !scanDataSegPrecisely();
75 _sections._gcRanges = (cast(void[]*) malloc(2 * (void[]).sizeof))[0..2];
77 auto databeg = cast(void*)&_xi_a;
78 auto dataend = cast(void*)_moduleinfo_array.ptr;
79 _sections._gcRanges[0] = databeg[0 .. dataend - databeg];
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];
88 size_t count = &_DPend - &_DPbegin;
89 auto ranges = cast(void[]*) malloc(count * (void[]).sizeof);
92 for (size_t i = 0; i < count; i++)
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];
98 ranges[r++] = (cast(void**)addr)[0..1];
101 _sections._gcRanges = ranges[0..r];
105 void finiSections() nothrow @nogc
107 free(_sections._gcRanges.ptr);
110 void[] initTLSRanges() nothrow @nogc
112 auto pbeg = cast(void*)&_tlsstart;
113 auto pend = cast(void*)&_tlsend;
114 return pbeg[0 .. pend - pbeg];
117 void finiTLSRanges(void[] rng) nothrow @nogc
121 void scanTLSRanges(void[] rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
125 dg(rng.ptr, rng.ptr + rng.length);
129 for (auto p = &_TPbegin; p < &_TPend; )
132 uint end = beg + cast(uint)((void*).sizeof);
133 while (p < &_TPend && *p == end)
135 end += (void*).sizeof;
138 dg(rng.ptr + beg, rng.ptr + end);
145 __gshared SectionGroup _sections;
147 // Windows: this gets initialized by minit.asm
148 extern(C) __gshared immutable(ModuleInfo*)[] _moduleinfo_array;
149 extern(C) void _minit() nothrow @nogc;
151 immutable(ModuleInfo*)[] getModuleInfos() nothrow @nogc
159 // _minit directly alters the global _moduleinfo_array
161 return _moduleinfo_array;
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
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