Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 932d1c0

Browse files
authored
Merge pull request #2725 from MartinNowak/merge_stable
Merge remote-tracking branch 'upstream/stable' into merge_stable merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents 8f32f19 + 47ded0e commit 932d1c0

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

changelog/darwin-loader.dd

Lines changed: 0 additions & 3 deletions
This file was deleted.

mak/DOCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,6 @@ DOCS=\
174174
$(DOCDIR)\rt_util_container_common.html \
175175
$(DOCDIR)\rt_util_container_hashtab.html \
176176
$(DOCDIR)\rt_util_container_treap.html \
177+
$(DOCDIR)\rt_util_utility.html \
177178
$(DOCDIR)\rt_util_random.html \
178179
$(DOCDIR)\rt_util_typeinfo.html \

mak/SRCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ SRCS=\
468468
\
469469
src\rt\util\random.d \
470470
src\rt\util\typeinfo.d \
471+
src\rt\util\utility.d \
471472
src\rt\util\container\array.d \
472473
src\rt\util\container\common.d \
473474
src\rt\util\container\hashtab.d \

src/rt/sections_elf_shared.d

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,7 @@ import rt.dmain2;
5959
import rt.minfo;
6060
import rt.util.container.array;
6161
import rt.util.container.hashtab;
62-
63-
/****
64-
* Asserts the specified condition, independent from -release, by abort()ing.
65-
* Regular assertions throw an AssertError and thus require an initialized
66-
* GC, which isn't the case (yet or anymore) for the startup/shutdown code in
67-
* this module (called by CRT ctors/dtors etc.).
68-
*/
69-
private void safeAssert(bool condition, scope string msg, size_t line = __LINE__) @nogc nothrow @safe
70-
{
71-
import core.internal.abort;
72-
condition || abort(msg, __FILE__, line);
73-
}
62+
import rt.util.utility : safeAssert;
7463

7564
alias DSO SectionGroup;
7665
struct DSO

src/rt/sections_osx_x86_64.d

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import core.stdc.string, core.stdc.stdlib;
2929
import core.sys.posix.pthread;
3030
import core.sys.darwin.mach.dyld;
3131
import core.sys.darwin.mach.getsect;
32+
3233
import rt.deh, rt.minfo;
3334
import rt.util.container.array;
35+
import rt.util.utility : safeAssert;
3436

3537
struct SectionGroup
3638
{
@@ -95,9 +97,11 @@ void finiSections() nothrow @nogc
9597

9698
void[] 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

103107
void finiTLSRanges(void[] rng) nothrow @nogc
@@ -169,7 +173,7 @@ static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
169173
ubyte[] 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

182186
extern (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
*/
266246
dyld_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;

src/rt/util/utility.d

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Contains various utility functions used by the runtime implementation.
3+
*
4+
* Copyright: Copyright Digital Mars 2016.
5+
* License: Distributed under the
6+
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
* (See accompanying file LICENSE)
8+
* Authors: Jacob Carlborg
9+
* Source: $(DRUNTIMESRC rt/util/_utility.d)
10+
*/
11+
module rt.util.utility;
12+
13+
/**
14+
* Asserts that the given condition is `true`.
15+
*
16+
* The assertion is independent from -release, by abort()ing. Regular assertions
17+
* throw an AssertError and thus require an initialized GC, which might not be
18+
* the case (yet or anymore) for the startup/shutdown code in this package
19+
* (called by CRT ctors/dtors etc.).
20+
*/
21+
package(rt) void safeAssert(
22+
bool condition, scope string msg, scope string file = __FILE__, size_t line = __LINE__
23+
) nothrow @nogc @safe
24+
{
25+
import core.internal.abort;
26+
condition || abort(msg, file, line);
27+
}

0 commit comments

Comments
 (0)