Skip to content

Commit 78958da

Browse files
[wasm] Add metadata registration SwiftRT-WASM.cpp
This patch adds the metadata registration for the wasm targets, and also adds build support for it.
1 parent 4e2f3b1 commit 78958da

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

stdlib/public/runtime/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(swift_runtime_backtracing_sources
8888
set(LLVM_OPTIONAL_SOURCES
8989
SwiftRT-COFF.cpp
9090
SwiftRT-ELF.cpp
91+
SwiftRT-WASM.cpp
9192
${swift_runtime_sources}
9293
${swift_runtime_objc_sources}
9394
${swift_runtime_leaks_sources}
@@ -138,11 +139,14 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY
138139

139140
set(ELFISH_SDKS)
140141
set(COFF_SDKS)
142+
set(WASM_SDKS)
141143
foreach(sdk ${SWIFT_SDKS})
142144
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
143145
list(APPEND ELFISH_SDKS ${sdk})
144146
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
145147
list(APPEND COFF_SDKS ${sdk})
148+
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM")
149+
list(APPEND WASM_SDKS ${sdk})
146150
endif()
147151
endforeach()
148152

@@ -169,13 +173,23 @@ add_swift_target_library(swiftImageRegistrationObjectCOFF
169173
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
170174
INSTALL_IN_COMPONENT none)
171175

176+
add_swift_target_library(swiftImageRegistrationObjectWASM
177+
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
178+
SwiftRT-WASM.cpp
179+
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
180+
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
181+
TARGET_SDKS ${WASM_SDKS}
182+
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
183+
INSTALL_IN_COMPONENT none)
184+
172185
foreach(sdk ${SWIFT_SDKS})
173186
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
174187
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
175188
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
176189

177190
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR
178-
"${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
191+
"${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF" OR
192+
"${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM")
179193
# TODO(compnerd) switch to the generator expression when cmake is upgraded
180194
# to a version which supports it.
181195
# set(swiftrtObject "$<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${sdk}_OBJECT_FORMAT}-${arch_suffix}>")
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===--- SwiftRT-WASM.cpp --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "ImageInspectionCommon.h"
14+
#include "swift/shims/MetadataSections.h"
15+
16+
#include <cstddef>
17+
#include <new>
18+
19+
// Refer start/stop symbols weakly to link them if they aren't synthesized by the linker.
20+
#define DECLARE_SWIFT_SECTION(name) \
21+
__attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __start_##name; \
22+
__attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __stop_##name;
23+
24+
extern "C" {
25+
DECLARE_SWIFT_SECTION(swift5_protocols)
26+
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
27+
DECLARE_SWIFT_SECTION(swift5_type_metadata)
28+
29+
DECLARE_SWIFT_SECTION(swift5_typeref)
30+
DECLARE_SWIFT_SECTION(swift5_reflstr)
31+
DECLARE_SWIFT_SECTION(swift5_fieldmd)
32+
DECLARE_SWIFT_SECTION(swift5_assocty)
33+
DECLARE_SWIFT_SECTION(swift5_replace)
34+
DECLARE_SWIFT_SECTION(swift5_replac2)
35+
DECLARE_SWIFT_SECTION(swift5_builtin)
36+
DECLARE_SWIFT_SECTION(swift5_capture)
37+
DECLARE_SWIFT_SECTION(swift5_mpenum)
38+
DECLARE_SWIFT_SECTION(swift5_accessible_functions)
39+
DECLARE_SWIFT_SECTION(swift5_runtime_attributes)
40+
}
41+
42+
#undef DECLARE_SWIFT_SECTION
43+
44+
namespace {
45+
static swift::MetadataSections sections{};
46+
}
47+
48+
__attribute__((__constructor__))
49+
static void swift_image_constructor() {
50+
#define SWIFT_SECTION_RANGE(name) \
51+
{ reinterpret_cast<uintptr_t>(&__start_##name), \
52+
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }
53+
54+
new (&sections) swift::MetadataSections {
55+
swift::CurrentSectionMetadataVersion,
56+
// NOTE: Multi images in a single process is not yet
57+
// stabilized in WebAssembly toolchain outside of Emscripten.
58+
{ 0 },
59+
60+
nullptr,
61+
nullptr,
62+
63+
SWIFT_SECTION_RANGE(swift5_protocols),
64+
SWIFT_SECTION_RANGE(swift5_protocol_conformances),
65+
SWIFT_SECTION_RANGE(swift5_type_metadata),
66+
67+
SWIFT_SECTION_RANGE(swift5_typeref),
68+
SWIFT_SECTION_RANGE(swift5_reflstr),
69+
SWIFT_SECTION_RANGE(swift5_fieldmd),
70+
SWIFT_SECTION_RANGE(swift5_assocty),
71+
SWIFT_SECTION_RANGE(swift5_replace),
72+
SWIFT_SECTION_RANGE(swift5_replac2),
73+
SWIFT_SECTION_RANGE(swift5_builtin),
74+
SWIFT_SECTION_RANGE(swift5_capture),
75+
SWIFT_SECTION_RANGE(swift5_mpenum),
76+
SWIFT_SECTION_RANGE(swift5_accessible_functions),
77+
SWIFT_SECTION_RANGE(swift5_runtime_attributes),
78+
};
79+
80+
#undef SWIFT_SECTION_RANGE
81+
82+
swift_addNewDSOImage(&sections);
83+
}

0 commit comments

Comments
 (0)