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

Commit 02ab5d1

Browse files
committed
rt.sections_osx_x86_64: Refactor & move a helper to rt.sections_darwin_64
That's the only functionality LDC needs from that module, the other helpers used by LDC for 64-bit Darwin (not just for x86_64, but also AArch64) already reside in rt.sections_darwin_64.
1 parent 011d766 commit 02ab5d1

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/rt/sections_darwin_64.d

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
*/
1212
module rt.sections_darwin_64;
1313

14-
import core.sys.darwin.mach.dyld;
15-
import core.sys.darwin.mach.getsect;
16-
import core.sys.posix.pthread;
17-
1814
version (OSX)
1915
version = Darwin;
2016
else version (iOS)
@@ -27,6 +23,12 @@ else version (WatchOS)
2723
version (Darwin):
2824
version (D_LP64):
2925

26+
import core.sys.darwin.mach.dyld;
27+
import core.sys.darwin.mach.getsect;
28+
import core.sys.posix.pthread;
29+
30+
import rt.util.utility : safeAssert;
31+
3032
extern (C) size_t malloc_size(const void* ptr) nothrow @nogc;
3133

3234
/**
@@ -176,3 +178,28 @@ const(section_64)[] sections(const segment_command_64* segment) pure nothrow @no
176178
const firstSection = cast(const(section_64)*)(cast(ubyte*) segment + size);
177179
return firstSection[0 .. segment.nsects];
178180
}
181+
182+
/// Invokes the specified delegate for each (non-empty) data section.
183+
void foreachDataSection(in mach_header* header, intptr_t slide,
184+
scope void delegate(void[] sectionData) processor)
185+
{
186+
foreach (section; [ SECT_DATA, SECT_BSS, SECT_COMMON ])
187+
{
188+
auto data = getSection(header, slide, SEG_DATA.ptr, section.ptr);
189+
if (data !is null)
190+
processor(data);
191+
}
192+
}
193+
194+
/// Returns a section's memory range, or null if not found or empty.
195+
void[] getSection(in mach_header* header, intptr_t slide,
196+
in char* segmentName, in char* sectionName)
197+
{
198+
safeAssert(header.magic == MH_MAGIC_64, "Unsupported header.");
199+
auto sect = getsectbynamefromheader_64(cast(mach_header_64*) header,
200+
segmentName, sectionName);
201+
202+
if (sect !is null && sect.size > 0)
203+
return (cast(void*)sect.addr + slide)[0 .. cast(size_t) sect.size];
204+
return null;
205+
}

src/rt/sections_osx_x86_64.d

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import core.sys.posix.pthread;
3030
import core.sys.darwin.mach.dyld;
3131
import core.sys.darwin.mach.getsect;
3232

33-
import rt.deh, rt.minfo;
33+
import rt.deh;
34+
import rt.minfo;
35+
import rt.sections_darwin_64;
3436
import rt.util.container.array;
3537
import rt.util.utility : safeAssert;
36-
import rt.sections_darwin_64 : getTLSRange;
3738

3839
struct SectionGroup
3940
{
@@ -121,12 +122,7 @@ __gshared SectionGroup _sections;
121122

122123
extern (C) void sections_osx_onAddImage(const scope mach_header* h, intptr_t slide)
123124
{
124-
foreach (e; dataSegs)
125-
{
126-
auto sect = getSection(h, slide, e.seg.ptr, e.sect.ptr);
127-
if (sect != null)
128-
_sections._gcRanges.insertBack((cast(void*)sect.ptr)[0 .. sect.length]);
129-
}
125+
foreachDataSection(h, slide, (sectionData) { _sections._gcRanges.insertBack(sectionData); });
130126

131127
auto minfosect = getSection(h, slide, "__DATA", "__minfodata");
132128
if (minfosect != null)
@@ -160,26 +156,3 @@ extern (C) void sections_osx_onAddImage(const scope mach_header* h, intptr_t sli
160156
_sections._ehTables = p[0 .. len];
161157
}
162158
}
163-
164-
struct SegRef
165-
{
166-
string seg;
167-
string sect;
168-
}
169-
170-
static immutable SegRef[] dataSegs = [{SEG_DATA, SECT_DATA},
171-
{SEG_DATA, SECT_BSS},
172-
{SEG_DATA, SECT_COMMON}];
173-
174-
ubyte[] getSection(in mach_header* header, intptr_t slide,
175-
in char* segmentName, in char* sectionName)
176-
{
177-
safeAssert(header.magic == MH_MAGIC_64, "Unsupported header.");
178-
auto sect = getsectbynamefromheader_64(cast(mach_header_64*)header,
179-
segmentName,
180-
sectionName);
181-
182-
if (sect !is null && sect.size > 0)
183-
return (cast(ubyte*)sect.addr + slide)[0 .. cast(size_t)sect.size];
184-
return null;
185-
}

0 commit comments

Comments
 (0)