Skip to content

Commit db18dea

Browse files
authored
Add a SWIFT_RUNTIME_MACHO_NO_DYLD stdlib mode that doesn't dynamically look up sections in modules, and only assumes a single static module (#33441)
1 parent d463c79 commit db18dea

9 files changed

+127
-19
lines changed

stdlib/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ option(SWIFT_ENABLE_COMPATIBILITY_OVERRIDES
1717
"Support back-deploying compatibility fixes for newer apps running on older runtimes."
1818
TRUE)
1919

20+
option(SWIFT_RUNTIME_MACHO_NO_DYLD
21+
"Build stdlib assuming the runtime environment uses Mach-O but does not support dynamic modules."
22+
FALSE)
23+
2024
#
2125
# End of user-configurable options.
2226
#

stdlib/cmake/modules/AddSwiftStdlib.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ function(_add_target_variant_c_compile_flags)
302302
list(APPEND result "-DSWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES")
303303
endif()
304304

305+
if(SWIFT_RUNTIME_MACHO_NO_DYLD)
306+
list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD")
307+
endif()
308+
305309
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
306310
endfunction()
307311

stdlib/public/runtime/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(swift_runtime_sources
5050
ImageInspectionMachO.cpp
5151
ImageInspectionELF.cpp
5252
ImageInspectionCOFF.cpp
53+
ImageInspectionStatic.cpp
5354
KeyPaths.cpp
5455
KnownMetadata.cpp
5556
Metadata.cpp

stdlib/public/runtime/Errors.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,
137137

138138
void swift::dumpStackTraceEntry(unsigned index, void *framePC,
139139
bool shortOutput) {
140-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
140+
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING && !defined(SWIFT_RUNTIME_MACHO_NO_DYLD)
141141
SymbolInfo syminfo;
142142

143143
// 0 is failure for lookupSymbol

stdlib/public/runtime/ImageInspectionCommon.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,27 @@
1919
#ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H
2020
#define SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H
2121

22-
#if !defined(__MACH__)
22+
#if defined(__MACH__)
23+
24+
#include <mach-o/dyld.h>
25+
26+
/// The Mach-O section name for the section containing protocol descriptor
27+
/// references. This lives within SEG_TEXT.
28+
#define MachOProtocolsSection "__swift5_protos"
29+
/// The Mach-O section name for the section containing protocol conformances.
30+
/// This lives within SEG_TEXT.
31+
#define MachOProtocolConformancesSection "__swift5_proto"
32+
/// The Mach-O section name for the section containing type references.
33+
/// This lives within SEG_TEXT.
34+
#define MachOTypeMetadataRecordSection "__swift5_types"
35+
/// The Mach-O section name for the section containing dynamic replacements.
36+
/// This lives within SEG_TEXT.
37+
#define MachODynamicReplacementSection "__swift5_replace"
38+
#define MachODynamicReplacementSomeSection "__swift5_replac2"
39+
40+
#define MachOTextSegment "__TEXT"
41+
42+
#else
2343

2444
#if defined(__ELF__)
2545
#define SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING "swift_reflection_metadata_magic_string"

stdlib/public/runtime/ImageInspectionMachO.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
///
1919
//===----------------------------------------------------------------------===//
2020

21-
#if defined(__APPLE__) && defined(__MACH__)
21+
#if defined(__APPLE__) && defined(__MACH__) && \
22+
!defined(SWIFT_RUNTIME_MACHO_NO_DYLD)
2223

2324
#include "ImageInspection.h"
25+
#include "ImageInspectionCommon.h"
2426
#include "swift/Runtime/Config.h"
2527
#include <mach-o/dyld.h>
2628
#include <mach-o/getsect.h>
@@ -31,21 +33,17 @@
3133
using namespace swift;
3234

3335
namespace {
34-
/// The Mach-O section name for the section containing protocol descriptor
35-
/// references. This lives within SEG_TEXT.
36-
constexpr const char ProtocolsSection[] = "__swift5_protos";
37-
/// The Mach-O section name for the section containing protocol conformances.
38-
/// This lives within SEG_TEXT.
39-
constexpr const char ProtocolConformancesSection[] = "__swift5_proto";
40-
/// The Mach-O section name for the section containing type references.
41-
/// This lives within SEG_TEXT.
42-
constexpr const char TypeMetadataRecordSection[] = "__swift5_types";
43-
/// The Mach-O section name for the section containing dynamic replacements.
44-
/// This lives within SEG_TEXT.
45-
constexpr const char DynamicReplacementSection[] = "__swift5_replace";
46-
constexpr const char DynamicReplacementSomeSection[] = "__swift5_replac2";
47-
48-
constexpr const char TextSegment[] = SEG_TEXT;
36+
37+
constexpr const char ProtocolsSection[] = MachOProtocolsSection;
38+
constexpr const char ProtocolConformancesSection[] =
39+
MachOProtocolConformancesSection;
40+
constexpr const char TypeMetadataRecordSection[] =
41+
MachOTypeMetadataRecordSection;
42+
constexpr const char DynamicReplacementSection[] =
43+
MachODynamicReplacementSection;
44+
constexpr const char DynamicReplacementSomeSection[] =
45+
MachODynamicReplacementSomeSection;
46+
constexpr const char TextSegment[] = MachOTextSegment;
4947

5048
#if __POINTER_WIDTH__ == 64
5149
using mach_header_platform = mach_header_64;
@@ -182,4 +180,5 @@ void *swift::lookupSection(const char *segment, const char *section, size_t *out
182180

183181
#endif // #ifndef SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES
184182

185-
#endif // defined(__APPLE__) && defined(__MACH__)
183+
#endif // defined(__APPLE__) && defined(__MACH__) &&
184+
// !defined(SWIFT_RUNTIME_MACHO_NO_DYLD)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- ImageInspectionStatic.cpp - image inspection for static stdlib ---===//
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+
/// \file
14+
///
15+
/// Implementation of ImageInspection for static stdlib (no dynamic loader
16+
/// present) environments. Assumes that only a single image exists in memory.
17+
///
18+
//===----------------------------------------------------------------------===//
19+
20+
#if defined(__MACH__) && defined(SWIFT_RUNTIME_MACHO_NO_DYLD)
21+
22+
#include "ImageInspection.h"
23+
#include "ImageInspectionCommon.h"
24+
25+
using namespace swift;
26+
27+
#define GET_SECTION_START_AND_SIZE(start, size, _seg, _sec) \
28+
extern void *__s##_seg##_sec __asm("section$start$" _seg "$" _sec); \
29+
extern void *__e##_seg##_sec __asm("section$end$" _seg "$" _sec); \
30+
start = &__s##_seg##_sec; \
31+
size = (char *)&__e##_seg##_sec - (char *)&__s##_seg##_sec;
32+
33+
void swift::initializeProtocolLookup() {
34+
void *start;
35+
uintptr_t size;
36+
GET_SECTION_START_AND_SIZE(start, size, TextSegment, ProtocolsSection);
37+
if (start == nullptr || size == 0)
38+
return;
39+
addImageProtocolsBlockCallbackUnsafe(start, size);
40+
}
41+
42+
void swift::initializeProtocolConformanceLookup() {
43+
void *start;
44+
uintptr_t size;
45+
GET_SECTION_START_AND_SIZE(start, size, TextSegment,
46+
ProtocolConformancesSection);
47+
if (start == nullptr || size == 0)
48+
return;
49+
addImageProtocolConformanceBlockCallbackUnsafe(start, size);
50+
}
51+
void swift::initializeTypeMetadataRecordLookup() {
52+
void *start;
53+
uintptr_t size;
54+
GET_SECTION_START_AND_SIZE(start, size, TextSegment,
55+
TypeMetadataRecordSection);
56+
if (start == nullptr || size == 0)
57+
return;
58+
addImageTypeMetadataRecordBlockCallbackUnsafe(start, size);
59+
}
60+
61+
void swift::initializeDynamicReplacementLookup() {
62+
void *start1;
63+
uintptr_t size1;
64+
GET_SECTION_START_AND_SIZE(start1, size1, TextSegment,
65+
DynamicReplacementSection);
66+
if (start1 == nullptr || size1 == 0)
67+
return;
68+
void *start2;
69+
uintptr_t size2;
70+
GET_SECTION_START_AND_SIZE(start2, size2, TextSegment,
71+
DynamicReplacementSection);
72+
if (start2 == nullptr || size2 == 0)
73+
return;
74+
addImageDynamicReplacementBlockCallback(start1, size1, start2, size2);
75+
}
76+
77+
#endif // defined(__MACH__) && defined(SWIFT_RUNTIME_MACHO_NO_DYLD)

utils/build-presets.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,7 @@ build-swift-dynamic-stdlib=0
24252425
build-swift-static-stdlib=1
24262426
swift-objc-interop=0
24272427
swift-enable-compatibility-overrides=0
2428+
swift-runtime-macho-no-dyld=1
24282429

24292430
[preset: stdlib_S_standalone_minimal_macho_x86_64,build]
24302431
mixin-preset=

utils/build-script-impl

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ KNOWN_SETTINGS=(
194194
stdlib-deployment-targets "" "space-separated list of targets to configure the Swift standard library to be compiled or cross-compiled for"
195195
swift-objc-interop "" "whether to enable interoperability with Objective-C, default is 1 on Apple platfors, 0 otherwise"
196196
swift-enable-compatibility-overrides "1" "whether to support back-deploying compatibility fixes for newer apps running on older runtimes"
197+
swift-runtime-macho-no-dyld "0" "whether to build stdlib assuming the runtime environment does not support dynamic modules"
197198

198199
## FREESTANDING Stdlib Options
199200
swift-freestanding-sdk "" "which SDK to use when building the FREESTANDING stdlib"
@@ -1762,6 +1763,7 @@ for host in "${ALL_HOSTS[@]}"; do
17621763
-DSWIFT_STDLIB_USE_NONATOMIC_RC:BOOL=$(true_false "${SWIFT_STDLIB_USE_NONATOMIC_RC}")
17631764
-DSWIFT_ENABLE_COMPATIBILITY_OVERRIDES:BOOL=$(true_false "${SWIFT_ENABLE_COMPATIBILITY_OVERRIDES}")
17641765
-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
1766+
-DSWIFT_RUNTIME_MACHO_NO_DYLD:BOOL=$(true_false "${SWIFT_RUNTIME_MACHO_NO_DYLD}")
17651767
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
17661768
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"
17671769
-DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING="${native_swift_tools_path}"

0 commit comments

Comments
 (0)