Skip to content

Commit 885cd05

Browse files
committed
WebAssembly: add start/end objects for metadata
The WebAssembly linker, unlike ELF linkers, doesn't define __start and __stop symbols for sections. So we have to make our own. If you put an object first in the command line, its symbols are placed first; same for last in command line. So we make two files. This is the same method that Swift used to use on ELF platforms. run ./buildstartend.sh; these two objects must be linked at the start and the end. see the updated linkPlease.sh file.
1 parent 2d60fab commit 885cd05

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

buildstartend.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
/home/zhuowei/wasi-sdk/bin/clang++ -c swift_start.cpp
4+
/home/zhuowei/wasi-sdk/bin/clang++ -c swift_end.cpp

linkPlease.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copy to ../build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin
33
exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \
44
/home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi/crt1.o \
5+
/home/zhuowei/swift-source/swift/swift_start.o \
6+
../lib/swift_static/wasm/wasm32/swiftrt.o \
57
hello.o \
68
-L../lib/swift_static/wasm/wasm32 \
79
-L../lib/swift/wasm/wasm32 \
@@ -10,4 +12,6 @@ exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \
1012
-lswiftCore \
1113
-lc -lc++ -lc++abi -lswiftImageInspectionShared \
1214
-licuuc -licudata \
13-
/home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o --verbose
15+
/home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o \
16+
/home/zhuowei/swift-source/swift/swift_end.o \
17+
--verbose --no-gc-sections

stdlib/public/runtime/SwiftRT-ELF.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ static swift::MetadataSections sections{};
4343

4444
__attribute__((__constructor__))
4545
static void swift_image_constructor() {
46+
#ifndef __wasm__
4647
#define SWIFT_SECTION_RANGE(name) \
4748
{ reinterpret_cast<uintptr_t>(&__start_##name), \
4849
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }
50+
#else
51+
// WebAssembly hack: ok this should really go in its own file
52+
#define SWIFT_SECTION_RANGE(name) \
53+
{ reinterpret_cast<uintptr_t>(&__start_##name) + sizeof(void*), \
54+
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }
55+
#endif
4956

5057
sections = {
5158
swift::CurrentSectionMetadataVersion,

swift_end.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===--- SwiftRT-ELF.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 <cstddef>
14+
15+
// We synthesize the start/stop symbols ourselves.
16+
#define DECLARE_SWIFT_SECTION(name) \
17+
__attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __stop_##name = (void*)0xfacefeed; \
18+
19+
extern "C" {
20+
DECLARE_SWIFT_SECTION(swift5_protocols)
21+
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
22+
DECLARE_SWIFT_SECTION(swift5_type_metadata)
23+
24+
DECLARE_SWIFT_SECTION(swift5_typeref)
25+
DECLARE_SWIFT_SECTION(swift5_reflstr)
26+
DECLARE_SWIFT_SECTION(swift5_fieldmd)
27+
DECLARE_SWIFT_SECTION(swift5_assocty)
28+
DECLARE_SWIFT_SECTION(swift5_replace)
29+
}

swift_start.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===--- SwiftRT-ELF.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 <cstddef>
14+
15+
// We synthesize the start/stop symbols ourselves.
16+
#define DECLARE_SWIFT_SECTION(name) \
17+
__attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __start_##name = (void*)0xdeadbeef; \
18+
19+
extern "C" {
20+
DECLARE_SWIFT_SECTION(swift5_protocols)
21+
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
22+
DECLARE_SWIFT_SECTION(swift5_type_metadata)
23+
24+
DECLARE_SWIFT_SECTION(swift5_typeref)
25+
DECLARE_SWIFT_SECTION(swift5_reflstr)
26+
DECLARE_SWIFT_SECTION(swift5_fieldmd)
27+
DECLARE_SWIFT_SECTION(swift5_assocty)
28+
DECLARE_SWIFT_SECTION(swift5_replace)
29+
}

0 commit comments

Comments
 (0)