Skip to content

Commit 899103c

Browse files
committed
COFF: restructure metadata registration
Restructure the COFF metadata handling to use the linker section grouping to emit section start/stop markers in the appropriate location. This allows us to lookup the sections statically without having to the walk the entire image structure. Introduce a constructor for PE/COFF binaries. This will ensure that the registration occurs for all modules appropriately. This should resolve rdar://problem/19045112. The registration should occur prior to `DllMain` being invoked from `DllMainCRTStartup`.
1 parent d0339c2 commit 899103c

File tree

10 files changed

+288
-263
lines changed

10 files changed

+288
-263
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ function(_add_swift_library_single target name)
607607
set(SWIFTLIB_SINGLE_options
608608
SHARED STATIC OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE IS_SDK_OVERLAY
609609
TARGET_LIBRARY FORCE_BUILD_FOR_HOST_SDK
610-
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE FORCE_BUILD_OPTIMIZED)
610+
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE FORCE_BUILD_OPTIMIZED NOSWIFTRT)
611611
cmake_parse_arguments(SWIFTLIB_SINGLE
612612
"${SWIFTLIB_SINGLE_options}"
613613
"MODULE_TARGET;SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;DEPLOYMENT_VERSION_OSX;DEPLOYMENT_VERSION_IOS;DEPLOYMENT_VERSION_TVOS;DEPLOYMENT_VERSION_WATCHOS"
@@ -819,11 +819,15 @@ function(_add_swift_library_single target name)
819819
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
820820
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
821821
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES})
822-
if("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" AND SWIFTLIB_TARGET_LIBRARY)
823-
if("${libkind}" STREQUAL "SHARED")
822+
if(("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" OR
823+
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "COFF") AND
824+
SWIFTLIB_TARGET_LIBRARY)
825+
if("${libkind}" STREQUAL "SHARED" AND NOT SWIFTLIB_SINGLE_NOSWIFTRT)
824826
# TODO(compnerd) switch to the generator expression when cmake is upgraded
825827
# to a version which supports it.
826-
# target_sources(${target} PRIVATE $<TARGET_OBJECTS:swiftImageRegistrationObject-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
828+
# target_sources(${target}
829+
# PRIVATE
830+
# $<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
827831
target_sources(${target}
828832
PRIVATE
829833
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}")
@@ -1350,7 +1354,8 @@ function(add_swift_library name)
13501354
set(SWIFTLIB_options
13511355
SHARED STATIC OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE IS_SDK_OVERLAY
13521356
TARGET_LIBRARY FORCE_BUILD_FOR_HOST_SDK
1353-
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE HAS_SWIFT_CONTENT FORCE_BUILD_OPTIMIZED)
1357+
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE HAS_SWIFT_CONTENT
1358+
FORCE_BUILD_OPTIMIZED NOSWIFTRT)
13541359
cmake_parse_arguments(SWIFTLIB
13551360
"${SWIFTLIB_options}"
13561361
"INSTALL_IN_COMPONENT;DEPLOYMENT_VERSION_OSX;DEPLOYMENT_VERSION_IOS;DEPLOYMENT_VERSION_TVOS;DEPLOYMENT_VERSION_WATCHOS"
@@ -1623,6 +1628,7 @@ function(add_swift_library name)
16231628
${SWIFTLIB_TARGET_LIBRARY_keyword}
16241629
${SWIFTLIB_FORCE_BUILD_FOR_HOST_SDK_keyword}
16251630
${SWIFTLIB_FORCE_BUILD_OPTIMIZED_keyword}
1631+
${SWIFTLIB_NOSWIFTRT_keyword}
16261632
INSTALL_IN_COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}"
16271633
DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}"
16281634
DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}"

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,7 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
23892389
sectionName = "swift2_protocol_conformances";
23902390
break;
23912391
case llvm::Triple::COFF:
2392-
sectionName = ".sw2prtc";
2392+
sectionName = ".sw2prtc$B";
23932393
break;
23942394
default:
23952395
llvm_unreachable("Don't know how to emit protocol conformances for "
@@ -2412,7 +2412,7 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
24122412
sectionName = "swift2_type_metadata";
24132413
break;
24142414
case llvm::Triple::COFF:
2415-
sectionName = ".sw2tymd";
2415+
sectionName = ".sw2tymd$B";
24162416
break;
24172417
default:
24182418
llvm_unreachable("Don't know how to emit type metadata table for "

lib/IRGen/GenReflection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ static std::string getReflectionSectionName(IRGenModule &IGM,
766766
case llvm::Triple::COFF:
767767
assert(FourCC.size() <= 4 &&
768768
"COFF section name length must be <= 8 characters");
769-
OS << ".sw3" << FourCC;
769+
OS << ".sw3" << FourCC << "$B";
770770
break;
771771
case llvm::Triple::ELF:
772772
OS << "swift3_" << LongName;

stdlib/public/SwiftRemoteMirror/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_
44
# libswiftRemoteMirror.dylib should not have runtime dependencies; it's
55
# always built as a shared library.
66
if(SWIFT_BUILD_DYNAMIC_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
7-
add_swift_library(swiftRemoteMirror SHARED TARGET_LIBRARY DONT_EMBED_BITCODE
7+
add_swift_library(swiftRemoteMirror SHARED TARGET_LIBRARY DONT_EMBED_BITCODE NOSWIFTRT
88
SwiftRemoteMirror.cpp
99
LINK_LIBRARIES swiftReflection
1010
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CXX_FLAGS}

stdlib/public/runtime/CMakeLists.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ set(swift_runtime_sources
4545
HeapObject.cpp
4646
ImageInspectionMachO.cpp
4747
ImageInspectionELF.cpp
48-
ImageInspectionWin32.cpp
48+
ImageInspectionCOFF.cpp
4949
KnownMetadata.cpp
5050
Metadata.cpp
5151
MetadataLookup.cpp
@@ -71,6 +71,7 @@ set(LLVM_OPTIONAL_SOURCES
7171
CygwinPort.cpp
7272
ImageInspectionELF.cpp
7373
StaticBinaryELF.cpp
74+
SwiftRT-COFF.cpp
7475
SwiftRT-ELF.cpp
7576
${swift_runtime_sources}
7677
${swift_runtime_objc_sources}
@@ -128,30 +129,41 @@ add_swift_library(swiftRuntime OBJECT_LIBRARY TARGET_LIBRARY
128129
INSTALL_IN_COMPONENT never_install)
129130

130131
set(ELFISH_SDKS)
132+
set(COFF_SDKS)
131133
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
132134
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
133135
list(APPEND ELFISH_SDKS ${sdk})
136+
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
137+
list(APPEND COFF_SDKS ${sdk})
134138
endif()
135139
endforeach()
136140

137-
add_swift_library(swiftImageRegistrationObject
141+
add_swift_library(swiftImageRegistrationObjectELF
138142
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
139143
SwiftRT-ELF.cpp
140144
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
141145
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
142146
TARGET_SDKS ${ELFISH_SDKS}
143147
INSTALL_IN_COMPONENT none)
148+
add_swift_library(swiftImageRegistrationObjectCOFF
149+
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
150+
SwiftRT-COFF.cpp
151+
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
152+
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
153+
TARGET_SDKS ${COFF_SDKS}
154+
INSTALL_IN_COMPONENT none)
144155

145156
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
146157
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
147158
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
148159
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
149160

150-
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
161+
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR
162+
"${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
151163
# TODO(compnerd) switch to the generator expression when cmake is upgraded
152164
# to a version which supports it.
153-
# set(swiftrtObject "$<TARGET_OBJECTS:swiftImageRegistrationObject-${arch_suffix}>")
154-
set(swiftrtObject ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/swiftImageRegistrationObject-${arch_suffix}.dir/SwiftRT-ELF.cpp${CMAKE_C_OUTPUT_EXTENSION})
165+
# set(swiftrtObject "$<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${sdk}_OBJECT_FORMAT}-${arch_suffix}>")
166+
set(swiftrtObject ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/swiftImageRegistrationObject${SWIFT_SDK_${sdk}_OBJECT_FORMAT}-${arch_suffix}.dir/SwiftRT-${SWIFT_SDK_${sdk}_OBJECT_FORMAT}.cpp${CMAKE_C_OUTPUT_EXTENSION})
155167
set(swiftrtPath "${SWIFTLIB_DIR}/${arch_subdir}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}")
156168

157169
add_custom_command_target(swiftImageRegistration-${arch_suffix}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===--- ImageInspectionWin32.cpp - Win32 image inspection ----------------===//
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+
#if !defined(__ELF__) && !defined(__MACH__)
14+
15+
#include "ImageInspection.h"
16+
#include "ImageInspectionCOFF.h"
17+
18+
#if defined(__CYGWIN__)
19+
#include <dlfcn.h>
20+
#endif
21+
22+
using namespace swift;
23+
24+
namespace {
25+
static const swift::MetadataSections *registered = nullptr;
26+
27+
void record(const swift::MetadataSections *sections) {
28+
if (registered == nullptr) {
29+
registered = sections;
30+
sections->next = sections->prev = sections;
31+
} else {
32+
registered->prev->next = sections;
33+
sections->next = registered;
34+
sections->prev = registered->prev;
35+
registered->prev = sections;
36+
}
37+
}
38+
}
39+
40+
void swift::initializeProtocolConformanceLookup() {
41+
const swift::MetadataSections *sections = registered;
42+
while (true) {
43+
const swift::MetadataSections::Range &conformances =
44+
sections->swift2_protocol_conformances;
45+
if (conformances.length)
46+
addImageProtocolConformanceBlockCallback(reinterpret_cast<void *>(conformances.start),
47+
conformances.length);
48+
49+
if (sections->next == registered)
50+
break;
51+
sections = sections->next;
52+
}
53+
}
54+
55+
void swift::initializeTypeMetadataRecordLookup() {
56+
const swift::MetadataSections *sections = registered;
57+
while (true) {
58+
const swift::MetadataSections::Range &type_metadata =
59+
sections->swift2_type_metadata;
60+
if (type_metadata.length)
61+
addImageTypeMetadataRecordBlockCallback(reinterpret_cast<void *>(type_metadata.start),
62+
type_metadata.length);
63+
64+
if (sections->next == registered)
65+
break;
66+
sections = sections->next;
67+
}
68+
}
69+
70+
SWIFT_RUNTIME_EXPORT
71+
void swift_addNewDSOImage(const void *addr) {
72+
const swift::MetadataSections *sections =
73+
static_cast<const swift::MetadataSections *>(addr);
74+
75+
record(sections);
76+
77+
const auto &protocol_conformances = sections->swift2_protocol_conformances;
78+
const void *conformances =
79+
reinterpret_cast<void *>(protocol_conformances.start);
80+
if (protocol_conformances.length)
81+
addImageProtocolConformanceBlockCallback(conformances,
82+
protocol_conformances.length);
83+
84+
const auto &type_metadata = sections->swift2_type_metadata;
85+
const void *metadata = reinterpret_cast<void *>(type_metadata.start);
86+
if (type_metadata.length)
87+
addImageTypeMetadataRecordBlockCallback(metadata, type_metadata.length);
88+
}
89+
90+
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
91+
#if defined(__CYGWIN__)
92+
Dl_info dlinfo;
93+
if (dladdr(address, &dlinfo) == 0) {
94+
return 0;
95+
}
96+
97+
info->fileName = dlinfo.dli_fname;
98+
info->baseAddress = dlinfo.dli_fbase;
99+
info->symbolName = dli_info.dli_sname;
100+
info->symbolAddress = dli_saddr;
101+
return 1;
102+
#else
103+
return 0;
104+
#endif // __CYGWIN__
105+
}
106+
107+
#endif // !defined(__ELF__) && !defined(__MACH__)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- ImageInspectionELF.h -----------------------------------*- C++ -*-===//
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+
/// COFF specific image inspection routines.
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H
20+
#define SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H
21+
22+
#if !defined(__ELF__) && !defined(__MACH__)
23+
24+
#include "../SwiftShims/Visibility.h"
25+
#include <cstdint>
26+
#include <cstddef>
27+
28+
namespace swift {
29+
struct SectionInfo {
30+
uint64_t size;
31+
const char *data;
32+
};
33+
34+
static constexpr const uintptr_t CurrentSectionMetadataVersion = 1;
35+
36+
struct MetadataSections {
37+
uintptr_t version;
38+
uintptr_t reserved;
39+
40+
mutable const MetadataSections *next;
41+
mutable const MetadataSections *prev;
42+
43+
struct Range {
44+
uintptr_t start;
45+
size_t length;
46+
};
47+
48+
Range swift2_protocol_conformances;
49+
Range swift2_type_metadata;
50+
Range swift3_typeref;
51+
Range swift3_reflstr;
52+
Range swift3_fieldmd;
53+
Range swift3_assocty;
54+
};
55+
} // namespace swift
56+
57+
// Called by injected constructors when a dynamic library is loaded.
58+
SWIFT_RUNTIME_EXPORT
59+
void swift_addNewDSOImage(const void *addr);
60+
61+
#endif // !defined(__ELF__) && !defined(__MACH__)
62+
63+
#endif // SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H

0 commit comments

Comments
 (0)