2 * Written in the D programming language.
3 * This module provides OSX-specific support for sections.
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)
13 module rt.sections_osx;
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;
28 static int opApply(scope int delegate(ref SectionGroup) dg)
33 static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
38 @property immutable(ModuleInfo*)[] modules() const
40 return _moduleGroup.modules;
43 @property ref inout(ModuleGroup) moduleGroup() inout
48 @property inout(void[])[] gcRanges() inout
53 @property immutable(FuncTable)[] ehTables() const
59 immutable(FuncTable)[] _ehTables;
60 ModuleGroup _moduleGroup;
61 Array!(void[]) _gcRanges;
62 immutable(void)[][2] _tlsImage;
66 * Boolean flag set to true while the runtime is initialized.
68 __gshared bool _isRuntimeInitialized;
71 * Gets called on program startup just before GC is initialized.
75 pthread_key_create(&_tlsKey, null);
76 _dyld_register_func_for_add_image(§ions_osx_onAddImage);
77 _isRuntimeInitialized = true;
81 * Gets called on program shutdown just after GC is terminated.
85 _sections._gcRanges.reset();
86 pthread_key_delete(_tlsKey);
87 _isRuntimeInitialized = false;
90 void[]* initTLSRanges()
92 return &getTLSBlock();
95 void finiTLSRanges(void[]* rng)
101 void scanTLSRanges(void[]* rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
103 dg(rng.ptr, rng.ptr + rng.length);
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.
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.
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 )
117 immutable off = tlsOffset(p);
118 auto tls = getTLSBlockAlloc();
119 assert(off < tls.length);
120 return tls.ptr + off;
125 __gshared pthread_key_t _tlsKey;
127 size_t tlsOffset(void* p)
130 assert(_sections._tlsImage[0].ptr !is null ||
131 _sections._tlsImage[1].ptr !is null);
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)
142 immutable off1 = cast(size_t)(p - _sections._tlsImage[1].ptr);
143 if (off1 < _sections._tlsImage[1].length)
145 size_t sz = (_sections._tlsImage[0].length + 15) & ~cast(size_t)15;
151 ref void[] getTLSBlock()
153 auto pary = cast(void[]*)pthread_getspecific(_tlsKey);
156 pary = cast(void[]*).calloc(1, (void[]).sizeof);
157 if (pthread_setspecific(_tlsKey, pary) != 0)
159 import core.stdc.stdio;
160 perror("pthread_setspecific failed with");
167 ref void[] getTLSBlockAlloc()
169 auto pary = &getTLSBlock();
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);
184 __gshared SectionGroup _sections;
186 extern (C) void sections_osx_onAddImage(in mach_header* h, intptr_t slide)
188 foreach (e; dataSegs)
190 auto sect = getSection(h, slide, e.seg.ptr, e.sect.ptr);
192 _sections._gcRanges.insertBack((cast(void*)sect.ptr)[0 .. sect.length]);
195 auto minfosect = getSection(h, slide, "__DATA", "__minfodata");
196 if (minfosect != null)
198 // no support for multiple images yet
199 // take the sections from the last static image which is the executable
200 if (_isRuntimeInitialized)
202 fprintf(stderr, "Loading shared libraries isn't yet supported on OSX.\n");
205 else if (_sections.modules.ptr !is null)
207 fprintf(stderr, "Shared libraries are not yet supported on OSX.\n");
210 debug(PRINTF) printf(" minfodata\n");
211 auto p = cast(immutable(ModuleInfo*)*)minfosect.ptr;
212 immutable len = minfosect.length / (*p).sizeof;
214 _sections._moduleGroup = ModuleGroup(p[0 .. len]);
217 auto ehsect = getSection(h, slide, "__DATA", "__deh_eh");
220 debug(PRINTF) printf(" deh_eh\n");
221 auto p = cast(immutable(FuncTable)*)ehsect.ptr;
222 immutable len = ehsect.length / (*p).sizeof;
224 _sections._ehTables = p[0 .. len];
227 auto tlssect = getSection(h, slide, "__DATA", "__tls_data");
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];
234 auto tlssect2 = getSection(h, slide, "__DATA", "__tlscoal_nt");
235 if (tlssect2 != null)
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];
249 static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
250 {SEG_DATA, SECT_BSS},
251 {SEG_DATA, SECT_COMMON}];
254 ubyte[] getSection(in mach_header* header, intptr_t slide,
255 in char* segmentName, in char* sectionName)
259 assert(header.magic == MH_MAGIC);
260 auto sect = getsectbynamefromheader(header,
264 else version (X86_64)
266 assert(header.magic == MH_MAGIC_64);
267 auto sect = getsectbynamefromheader_64(cast(mach_header_64*)header,
272 static assert(0, "unimplemented");
274 if (sect !is null && sect.size > 0)
275 return (cast(ubyte*)sect.addr + slide)[0 .. cast(size_t)sect.size];