@@ -29,8 +29,10 @@ import core.stdc.string, core.stdc.stdlib;
2929import core.sys.posix.pthread ;
3030import core.sys.darwin.mach.dyld ;
3131import core.sys.darwin.mach.getsect ;
32+
3233import rt.deh, rt.minfo;
3334import rt.util.container.array;
35+ import rt.util.utility : safeAssert;
3436
3537struct SectionGroup
3638{
@@ -95,9 +97,11 @@ void finiSections() nothrow @nogc
9597
9698void [] initTLSRanges () nothrow @nogc
9799{
98- auto range = getTLSRange();
99- assert (range.isValid, " Could not determine TLS range." );
100- return range.toArray;
100+ static ubyte tlsAnchor;
101+
102+ auto range = getTLSRange(&tlsAnchor);
103+ safeAssert(range ! is null , " Could not determine TLS range." );
104+ return range;
101105}
102106
103107void finiTLSRanges (void [] rng) nothrow @nogc
@@ -169,7 +173,7 @@ static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
169173ubyte [] getSection (in mach_header* header, intptr_t slide,
170174 in char * segmentName, in char * sectionName)
171175{
172- assert (header.magic == MH_MAGIC_64 );
176+ safeAssert (header.magic == MH_MAGIC_64 , " Unsupported header. " );
173177 auto sect = getsectbynamefromheader_64(cast (mach_header_64* )header,
174178 segmentName,
175179 sectionName);
@@ -181,44 +185,22 @@ ubyte[] getSection(in mach_header* header, intptr_t slide,
181185
182186extern (C ) size_t malloc_size(const void * ptr) nothrow @nogc ;
183187
184- // / Represents a TLS range.
185- struct TLSRange
186- {
187- // / The start of the range.
188- void * start;
189-
190- // / The size of the range.
191- size_t size;
192-
193- // / Returns `true` if the range is valid.
194- bool isValid () const pure nothrow @nogc @safe
195- {
196- return start ! is null && size > 0 ;
197- }
198-
199- // / Returns the range as an array.
200- void [] toArray () pure nothrow @nogc
201- {
202- return start[0 .. size];
203- }
204- }
205-
206- // / Returns the TLS range of the current image.
207- TLSRange getTLSRange () nothrow @nogc
188+ /**
189+ * Returns the TLS range of the image containing the specified TLS symbol,
190+ * or null if none was found.
191+ */
192+ void [] getTLSRange (const void * tlsSymbol) nothrow @nogc
208193{
209- static ubyte tlsAnchor;
210- const tlsSymbol = &tlsAnchor;
211-
212194 foreach (i ; 0 .. _dyld_image_count)
213195 {
214196 const header = cast (const (mach_header_64)* ) _dyld_get_image_header(i);
215197 auto tlvInfo = tlvInfo(header);
216198
217199 if (tlvInfo.foundTLSRange(tlsSymbol))
218- return TLSRange ( tlvInfo.tlv_addr, tlvInfo.tlv_size) ;
200+ return tlvInfo.tlv_addr[ 0 .. tlvInfo.tlv_size] ;
219201 }
220202
221- return TLSRange.init ;
203+ return null ;
222204}
223205
224206/**
@@ -249,29 +231,27 @@ struct dyld_tlv_info
249231 // / Base address of TLV storage
250232 void * tlv_addr;
251233
252- // Byte size of TLV storage
234+ // / Byte size of TLV storage
253235 size_t tlv_size;
254236}
255237
256238/**
257239 * Returns the TLV info for the given image.
258240 *
259- * Asserts if no TLV address could be found.
260- *
261241 * Params:
262242 * header = the image to look for the TLV info in
263243 *
264244 * Returns: the TLV info
265245 */
266246dyld_tlv_info tlvInfo (const mach_header_64* header) nothrow @nogc
267247{
268- auto tlvAddress = pthread_getspecific( header.firstTLVKey) ;
269- assert ( tlvAddress, " No TLV address found " );
248+ const key = header.firstTLVKey;
249+ auto tlvAddress = key == pthread_key_t .max ? null : pthread_getspecific(key );
270250
271251 dyld_tlv_info info = {
272252 info_size: dyld_tlv_info.sizeof,
273253 tlv_addr: tlvAddress,
274- tlv_size: malloc_size (tlvAddress)
254+ tlv_size: tlvAddress ? malloc_size(tlvAddress) : 0
275255 };
276256
277257 return info;
0 commit comments