1 // OSX-specific support for sections.
2 // Copyright (C) 2019-2021 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.osx;
28 import core.stdc.stdio;
29 import core.stdc.string, core.stdc.stdlib;
30 import core.sys.posix.pthread;
31 import core.sys.darwin.mach.dyld;
32 import core.sys.darwin.mach.getsect;
33 import rt.deh, rt.minfo;
34 import rt.util.container.array;
38 static int opApply(scope int delegate(ref SectionGroup) dg)
43 static int opApplyReverse(scope int delegate(ref SectionGroup) dg)
48 @property immutable(ModuleInfo*)[] modules() const nothrow @nogc
50 return _moduleGroup.modules;
53 @property ref inout(ModuleGroup) moduleGroup() inout nothrow @nogc
58 @property inout(void[])[] gcRanges() inout nothrow @nogc
63 @property immutable(FuncTable)[] ehTables() const nothrow @nogc
69 immutable(FuncTable)[] _ehTables;
70 ModuleGroup _moduleGroup;
71 Array!(void[]) _gcRanges;
72 immutable(void)[][2] _tlsImage;
76 * Boolean flag set to true while the runtime is initialized.
78 __gshared bool _isRuntimeInitialized;
81 * Gets called on program startup just before GC is initialized.
83 void initSections() nothrow @nogc
85 pthread_key_create(&_tlsKey, null);
86 _dyld_register_func_for_add_image(§ions_osx_onAddImage);
87 _isRuntimeInitialized = true;
91 * Gets called on program shutdown just after GC is terminated.
93 void finiSections() nothrow @nogc
95 _sections._gcRanges.reset();
96 pthread_key_delete(_tlsKey);
97 _isRuntimeInitialized = false;
100 void[]* initTLSRanges() nothrow @nogc
102 return &getTLSBlock();
105 void finiTLSRanges(void[]* rng) nothrow @nogc
111 void scanTLSRanges(void[]* rng, scope void delegate(void* pbeg, void* pend) nothrow dg) nothrow
113 dg(rng.ptr, rng.ptr + rng.length);
116 // NOTE: The Mach-O object file format does not allow for thread local
117 // storage declarations. So instead we roll our own by putting tls
118 // into the __tls_data and the __tlscoal_nt sections.
120 // This function is called by the code emitted by the compiler. It
121 // is expected to translate an address into the TLS static data to
122 // the corresponding address in the TLS dynamic per-thread data.
124 // NB: the compiler mangles this function as '___tls_get_addr' even though it is extern(D)
125 extern(D) void* ___tls_get_addr( void* p )
127 immutable off = tlsOffset(p);
128 auto tls = getTLSBlockAlloc();
129 assert(off < tls.length);
130 return tls.ptr + off;
135 __gshared pthread_key_t _tlsKey;
137 size_t tlsOffset(void* p)
140 assert(_sections._tlsImage[0].ptr !is null ||
141 _sections._tlsImage[1].ptr !is null);
145 // NOTE: p is an address in the TLS static data emitted by the
146 // compiler. If it isn't, something is disastrously wrong.
147 immutable off0 = cast(size_t)(p - _sections._tlsImage[0].ptr);
148 if (off0 < _sections._tlsImage[0].length)
152 immutable off1 = cast(size_t)(p - _sections._tlsImage[1].ptr);
153 if (off1 < _sections._tlsImage[1].length)
155 size_t sz = (_sections._tlsImage[0].length + 15) & ~cast(size_t)15;
161 ref void[] getTLSBlock() nothrow @nogc
163 auto pary = cast(void[]*)pthread_getspecific(_tlsKey);
166 pary = cast(void[]*).calloc(1, (void[]).sizeof);
167 if (pthread_setspecific(_tlsKey, pary) != 0)
169 import core.stdc.stdio;
170 perror("pthread_setspecific failed with");
177 ref void[] getTLSBlockAlloc()
179 auto pary = &getTLSBlock();
182 auto imgs = _sections._tlsImage;
183 immutable sz0 = (imgs[0].length + 15) & ~cast(size_t)15;
184 immutable sz2 = sz0 + imgs[1].length;
185 auto p = .malloc(sz2);
186 memcpy(p, imgs[0].ptr, imgs[0].length);
187 memcpy(p + sz0, imgs[1].ptr, imgs[1].length);
193 __gshared SectionGroup _sections;
195 extern (C) void sections_osx_onAddImage(in mach_header* h, intptr_t slide)
197 foreach (e; dataSegs)
199 auto sect = getSection(h, slide, e.seg.ptr, e.sect.ptr);
201 _sections._gcRanges.insertBack((cast(void*)sect.ptr)[0 .. sect.length]);
204 auto minfosect = getSection(h, slide, "__DATA", "__minfodata");
205 if (minfosect != null)
207 // no support for multiple images yet
208 // take the sections from the last static image which is the executable
209 if (_isRuntimeInitialized)
211 fprintf(stderr, "Loading shared libraries isn't yet supported on OSX.\n");
214 else if (_sections.modules.ptr !is null)
216 fprintf(stderr, "Shared libraries are not yet supported on OSX.\n");
219 debug(PRINTF) printf(" minfodata\n");
220 auto p = cast(immutable(ModuleInfo*)*)minfosect.ptr;
221 immutable len = minfosect.length / (*p).sizeof;
223 _sections._moduleGroup = ModuleGroup(p[0 .. len]);
226 auto ehsect = getSection(h, slide, "__DATA", "__deh_eh");
229 debug(PRINTF) printf(" deh_eh\n");
230 auto p = cast(immutable(FuncTable)*)ehsect.ptr;
231 immutable len = ehsect.length / (*p).sizeof;
233 _sections._ehTables = p[0 .. len];
236 auto tlssect = getSection(h, slide, "__DATA", "__tls_data");
239 debug(PRINTF) printf(" tls_data %p %p\n", tlssect.ptr, tlssect.ptr + tlssect.length);
240 _sections._tlsImage[0] = (cast(immutable(void)*)tlssect.ptr)[0 .. tlssect.length];
243 auto tlssect2 = getSection(h, slide, "__DATA", "__tlscoal_nt");
244 if (tlssect2 != null)
246 debug(PRINTF) printf(" tlscoal_nt %p %p\n", tlssect2.ptr, tlssect2.ptr + tlssect2.length);
247 _sections._tlsImage[1] = (cast(immutable(void)*)tlssect2.ptr)[0 .. tlssect2.length];
257 static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
258 {SEG_DATA, SECT_BSS},
259 {SEG_DATA, SECT_COMMON}];
261 ubyte[] getSection(in mach_header* header, intptr_t slide,
262 in char* segmentName, in char* sectionName)
266 assert(header.magic == MH_MAGIC);
267 auto sect = getsectbynamefromheader(header,
271 else version (X86_64)
273 assert(header.magic == MH_MAGIC_64);
274 auto sect = getsectbynamefromheader_64(cast(mach_header_64*)header,
279 static assert(0, "unimplemented");
281 if (sect !is null && sect.size > 0)
282 return (cast(ubyte*)sect.addr + slide)[0 .. cast(size_t)sect.size];