Skip to content

Commit 9d1b8d4

Browse files
committed
Don't use swift_enumerateAllMetadataSections() on WASI.
On WASI, there is only a single statically-linked binary, so we don't need to use `swift_enumerateAllMetadataSections()` and can just directly access the bounds of the metadata section(s) we care about.
1 parent baddcf0 commit 9d1b8d4

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

Sources/_TestingInternals/Discovery.cpp

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,8 @@ struct SWTTypeMetadataRecord {
213213
}
214214
};
215215

216-
#if defined(SWT_NO_DYNAMIC_LINKING)
217-
#pragma mark - Statically-linked implementation
218-
219-
// This environment does not have a dynamic linker/loader. Therefore, there is
220-
// only one image (this one) with Swift code in it.
221-
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp
222-
223-
extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
224-
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");
225-
226-
template <typename SectionEnumerator>
227-
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
228-
auto size = std::distance(&sectionBegin, &sectionEnd);
229-
bool stop = false;
230-
body(nullptr, &sectionBegin, size, &stop);
231-
}
232-
233-
#elif defined(__APPLE__)
216+
#if defined(__APPLE__)
217+
#if !defined(SWT_NO_DYNAMIC_LINKING)
234218
#pragma mark - Apple implementation
235219

236220
/// A type that acts as a C++ [Container](https://en.cppreference.com/w/cpp/named_req/Container)
@@ -317,6 +301,24 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
317301
}
318302
}
319303

304+
#else
305+
#pragma mark - Apple implementation (statically linked)
306+
307+
// This environment does not have a dynamic linker/loader. Therefore, there is
308+
// only one image (this one) with Swift code in it.
309+
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp
310+
311+
extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
312+
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");
313+
314+
template <typename SectionEnumerator>
315+
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
316+
auto size = std::distance(&sectionBegin, &sectionEnd);
317+
bool stop = false;
318+
body(nullptr, &sectionBegin, size, &stop);
319+
}
320+
#endif
321+
320322
#elif defined(_WIN32)
321323
#pragma mark - Windows implementation
322324

@@ -417,8 +419,26 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
417419
}
418420
}
419421

422+
#elif defined(__wasi__)
423+
#pragma mark - WASI implementation (statically linked)
424+
425+
extern "C" const char __start_swift5_type_metadata __attribute__((__visibility__("hidden"), __aligned__(1)));
426+
extern "C" const char __stop_swift5_type_metadata __attribute__((__visibility__("hidden"), __aligned__(1)));
427+
428+
template <typename SectionEnumerator>
429+
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
430+
const auto& sectionBegin = __start_swift5_type_metadata;
431+
const auto& sectionEnd = __stop_swift5_type_metadata;
432+
433+
// WASI only has a single image (so far) and it is statically linked, so all
434+
// Swift metadata ends up in the same section bounded by the named symbols
435+
// above. So we can just yield the section betwixt them.
436+
auto size = std::distance(&sectionBegin, &sectionEnd);
437+
bool stop = false;
438+
body(nullptr, &sectionBegin, size, &stop);
439+
}
420440

421-
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__wasi__) || defined(__ANDROID__)
441+
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__)
422442
#pragma mark - ELF implementation
423443

424444
/// Specifies the address range corresponding to a section.

0 commit comments

Comments
 (0)