Skip to content

Commit 05890bc

Browse files
authored
Don't use swift_enumerateAllMetadataSections() on WASI. (#730)
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. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent bfbae91 commit 05890bc

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

Sources/_TestingInternals/Discovery.cpp

Lines changed: 40 additions & 20 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

@@ -396,7 +398,7 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
396398
// modules do not support unloading, so we'll just not worry about them.)
397399
using SWTSectionList = SWTVector<std::tuple<HMODULE, const void *, size_t>>;
398400
SWTSectionList sectionList;
399-
for (DWORD i = 0; i < hModuleCount; i++) {
401+
for (size_t i = 0; i < hModuleCount; i++) {
400402
if (auto section = findSection(hModules[i], ".sw5tymd")) {
401403
sectionList.emplace_back(hModules[i], section->first, section->second);
402404
}
@@ -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;
426+
extern "C" const char __stop_swift5_type_metadata;
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)