]> git.ipfire.org Git - thirdparty/gcc.git/blob - libphobos/libdruntime/rt/sections_osx.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / libphobos / libdruntime / rt / sections_osx.d
1 /**
2 * Written in the D programming language.
3 * This module provides OSX-specific support for sections.
4 *
5 * Copyright: Copyright Digital Mars 2008 - 2012.
6 * License: Distributed under the
7 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
8 * (See accompanying file LICENSE)
9 * Authors: Walter Bright, Sean Kelly, Martin Nowak
10 * Source: $(DRUNTIMESRC src/rt/_sections_osx.d)
11 */
12
13 module rt.sections_osx;
14
15 version (OSX):
16
17 // debug = PRINTF;
18 import core.stdc.stdio;
19 import core.stdc.string, core.stdc.stdlib;
20 import core.sys.posix.pthread;
21 import core.sys.darwin.mach.dyld;
22 import core.sys.darwin.mach.getsect;
23 import rt.deh, rt.minfo;
24 import rt.util.container.array;
25
26 struct SectionGroup
27 {
28 static int opApply(scope int delegate(ref SectionGroup) dg)
29 {
30 return dg(_sections);
31 }
32
33 static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
34 {
35 return dg(_sections);
36 }
37
38 @property immutable(ModuleInfo*)[] modules() const
39 {
40 return _moduleGroup.modules;
41 }
42
43 @property ref inout(ModuleGroup) moduleGroup() inout
44 {
45 return _moduleGroup;
46 }
47
48 @property inout(void[])[] gcRanges() inout
49 {
50 return _gcRanges[];
51 }
52
53 @property immutable(FuncTable)[] ehTables() const
54 {
55 return _ehTables[];
56 }
57
58 private:
59 immutable(FuncTable)[] _ehTables;
60 ModuleGroup _moduleGroup;
61 Array!(void[]) _gcRanges;
62 immutable(void)[][2] _tlsImage;
63 }
64
65 /****
66 * Boolean flag set to true while the runtime is initialized.
67 */
68 __gshared bool _isRuntimeInitialized;
69
70 /****
71 * Gets called on program startup just before GC is initialized.
72 */
73 void initSections()
74 {
75 pthread_key_create(&_tlsKey, null);
76 _dyld_register_func_for_add_image(&sections_osx_onAddImage);
77 _isRuntimeInitialized = true;
78 }
79
80 /***
81 * Gets called on program shutdown just after GC is terminated.
82 */
83 void finiSections()
84 {
85 _sections._gcRanges.reset();
86 pthread_key_delete(_tlsKey);
87 _isRuntimeInitialized = false;
88 }
89
90 void[]* initTLSRanges()
91 {
92 return &getTLSBlock();
93 }
94
95 void finiTLSRanges(void[]* rng)
96 {
97 .free(rng.ptr);
98 .free(rng);
99 }
100
101 void scanTLSRanges(void[]* rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
102 {
103 dg(rng.ptr, rng.ptr + rng.length);
104 }
105
106 // NOTE: The Mach-O object file format does not allow for thread local
107 // storage declarations. So instead we roll our own by putting tls
108 // into the __tls_data and the __tlscoal_nt sections.
109 //
110 // This function is called by the code emitted by the compiler. It
111 // is expected to translate an address into the TLS static data to
112 // the corresponding address in the TLS dynamic per-thread data.
113
114 // NB: the compiler mangles this function as '___tls_get_addr' even though it is extern(D)
115 extern(D) void* ___tls_get_addr( void* p )
116 {
117 immutable off = tlsOffset(p);
118 auto tls = getTLSBlockAlloc();
119 assert(off < tls.length);
120 return tls.ptr + off;
121 }
122
123 private:
124
125 __gshared pthread_key_t _tlsKey;
126
127 size_t tlsOffset(void* p)
128 in
129 {
130 assert(_sections._tlsImage[0].ptr !is null ||
131 _sections._tlsImage[1].ptr !is null);
132 }
133 body
134 {
135 // NOTE: p is an address in the TLS static data emitted by the
136 // compiler. If it isn't, something is disastrously wrong.
137 immutable off0 = cast(size_t)(p - _sections._tlsImage[0].ptr);
138 if (off0 < _sections._tlsImage[0].length)
139 {
140 return off0;
141 }
142 immutable off1 = cast(size_t)(p - _sections._tlsImage[1].ptr);
143 if (off1 < _sections._tlsImage[1].length)
144 {
145 size_t sz = (_sections._tlsImage[0].length + 15) & ~cast(size_t)15;
146 return sz + off1;
147 }
148 assert(0);
149 }
150
151 ref void[] getTLSBlock()
152 {
153 auto pary = cast(void[]*)pthread_getspecific(_tlsKey);
154 if (pary is null)
155 {
156 pary = cast(void[]*).calloc(1, (void[]).sizeof);
157 if (pthread_setspecific(_tlsKey, pary) != 0)
158 {
159 import core.stdc.stdio;
160 perror("pthread_setspecific failed with");
161 assert(0);
162 }
163 }
164 return *pary;
165 }
166
167 ref void[] getTLSBlockAlloc()
168 {
169 auto pary = &getTLSBlock();
170 if (!pary.length)
171 {
172 auto imgs = _sections._tlsImage;
173 immutable sz0 = (imgs[0].length + 15) & ~cast(size_t)15;
174 immutable sz2 = sz0 + imgs[1].length;
175 auto p = .malloc(sz2);
176 memcpy(p, imgs[0].ptr, imgs[0].length);
177 memcpy(p + sz0, imgs[1].ptr, imgs[1].length);
178 *pary = p[0 .. sz2];
179 }
180 return *pary;
181 }
182
183
184 __gshared SectionGroup _sections;
185
186 extern (C) void sections_osx_onAddImage(in mach_header* h, intptr_t slide)
187 {
188 foreach (e; dataSegs)
189 {
190 auto sect = getSection(h, slide, e.seg.ptr, e.sect.ptr);
191 if (sect != null)
192 _sections._gcRanges.insertBack((cast(void*)sect.ptr)[0 .. sect.length]);
193 }
194
195 auto minfosect = getSection(h, slide, "__DATA", "__minfodata");
196 if (minfosect != null)
197 {
198 // no support for multiple images yet
199 // take the sections from the last static image which is the executable
200 if (_isRuntimeInitialized)
201 {
202 fprintf(stderr, "Loading shared libraries isn't yet supported on OSX.\n");
203 return;
204 }
205 else if (_sections.modules.ptr !is null)
206 {
207 fprintf(stderr, "Shared libraries are not yet supported on OSX.\n");
208 }
209
210 debug(PRINTF) printf(" minfodata\n");
211 auto p = cast(immutable(ModuleInfo*)*)minfosect.ptr;
212 immutable len = minfosect.length / (*p).sizeof;
213
214 _sections._moduleGroup = ModuleGroup(p[0 .. len]);
215 }
216
217 auto ehsect = getSection(h, slide, "__DATA", "__deh_eh");
218 if (ehsect != null)
219 {
220 debug(PRINTF) printf(" deh_eh\n");
221 auto p = cast(immutable(FuncTable)*)ehsect.ptr;
222 immutable len = ehsect.length / (*p).sizeof;
223
224 _sections._ehTables = p[0 .. len];
225 }
226
227 auto tlssect = getSection(h, slide, "__DATA", "__tls_data");
228 if (tlssect != null)
229 {
230 debug(PRINTF) printf(" tls_data %p %p\n", tlssect.ptr, tlssect.ptr + tlssect.length);
231 _sections._tlsImage[0] = (cast(immutable(void)*)tlssect.ptr)[0 .. tlssect.length];
232 }
233
234 auto tlssect2 = getSection(h, slide, "__DATA", "__tlscoal_nt");
235 if (tlssect2 != null)
236 {
237 debug(PRINTF) printf(" tlscoal_nt %p %p\n", tlssect2.ptr, tlssect2.ptr + tlssect2.length);
238 _sections._tlsImage[1] = (cast(immutable(void)*)tlssect2.ptr)[0 .. tlssect2.length];
239 }
240 }
241
242 struct SegRef
243 {
244 string seg;
245 string sect;
246 }
247
248
249 static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
250 {SEG_DATA, SECT_BSS},
251 {SEG_DATA, SECT_COMMON}];
252
253
254 ubyte[] getSection(in mach_header* header, intptr_t slide,
255 in char* segmentName, in char* sectionName)
256 {
257 version (X86)
258 {
259 assert(header.magic == MH_MAGIC);
260 auto sect = getsectbynamefromheader(header,
261 segmentName,
262 sectionName);
263 }
264 else version (X86_64)
265 {
266 assert(header.magic == MH_MAGIC_64);
267 auto sect = getsectbynamefromheader_64(cast(mach_header_64*)header,
268 segmentName,
269 sectionName);
270 }
271 else
272 static assert(0, "unimplemented");
273
274 if (sect !is null && sect.size > 0)
275 return (cast(ubyte*)sect.addr + slide)[0 .. cast(size_t)sect.size];
276 return null;
277 }