Skip to content

COFF: restructure metadata registration #13211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ function(_add_swift_library_single target name)
set(SWIFTLIB_SINGLE_options
SHARED STATIC OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE IS_SDK_OVERLAY
TARGET_LIBRARY FORCE_BUILD_FOR_HOST_SDK
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE FORCE_BUILD_OPTIMIZED)
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE FORCE_BUILD_OPTIMIZED NOSWIFTRT)
cmake_parse_arguments(SWIFTLIB_SINGLE
"${SWIFTLIB_SINGLE_options}"
"MODULE_TARGET;SDK;ARCHITECTURE;INSTALL_IN_COMPONENT;DEPLOYMENT_VERSION_OSX;DEPLOYMENT_VERSION_IOS;DEPLOYMENT_VERSION_TVOS;DEPLOYMENT_VERSION_WATCHOS"
Expand Down Expand Up @@ -819,11 +819,15 @@ function(_add_swift_library_single target name)
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES})
if("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" AND SWIFTLIB_TARGET_LIBRARY)
if("${libkind}" STREQUAL "SHARED")
if(("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" OR
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "COFF") AND
SWIFTLIB_TARGET_LIBRARY)
if("${libkind}" STREQUAL "SHARED" AND NOT SWIFTLIB_SINGLE_NOSWIFTRT)
# TODO(compnerd) switch to the generator expression when cmake is upgraded
# to a version which supports it.
# target_sources(${target} PRIVATE $<TARGET_OBJECTS:swiftImageRegistrationObject-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
# target_sources(${target}
# PRIVATE
# $<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
target_sources(${target}
PRIVATE
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}")
Expand Down Expand Up @@ -1350,7 +1354,8 @@ function(add_swift_library name)
set(SWIFTLIB_options
SHARED STATIC OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE IS_SDK_OVERLAY
TARGET_LIBRARY FORCE_BUILD_FOR_HOST_SDK
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE HAS_SWIFT_CONTENT FORCE_BUILD_OPTIMIZED)
API_NOTES_NON_OVERLAY DONT_EMBED_BITCODE HAS_SWIFT_CONTENT
FORCE_BUILD_OPTIMIZED NOSWIFTRT)
cmake_parse_arguments(SWIFTLIB
"${SWIFTLIB_options}"
"INSTALL_IN_COMPONENT;DEPLOYMENT_VERSION_OSX;DEPLOYMENT_VERSION_IOS;DEPLOYMENT_VERSION_TVOS;DEPLOYMENT_VERSION_WATCHOS"
Expand Down Expand Up @@ -1623,6 +1628,7 @@ function(add_swift_library name)
${SWIFTLIB_TARGET_LIBRARY_keyword}
${SWIFTLIB_FORCE_BUILD_FOR_HOST_SDK_keyword}
${SWIFTLIB_FORCE_BUILD_OPTIMIZED_keyword}
${SWIFTLIB_NOSWIFTRT_keyword}
INSTALL_IN_COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}"
DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}"
DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}"
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
sectionName = "swift2_protocol_conformances";
break;
case llvm::Triple::COFF:
sectionName = ".sw2prtc";
sectionName = ".sw2prtc$B";
break;
default:
llvm_unreachable("Don't know how to emit protocol conformances for "
Expand All @@ -2412,7 +2412,7 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
sectionName = "swift2_type_metadata";
break;
case llvm::Triple::COFF:
sectionName = ".sw2tymd";
sectionName = ".sw2tymd$B";
break;
default:
llvm_unreachable("Don't know how to emit type metadata table for "
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ static std::string getReflectionSectionName(IRGenModule &IGM,
case llvm::Triple::COFF:
assert(FourCC.size() <= 4 &&
"COFF section name length must be <= 8 characters");
OS << ".sw3" << FourCC;
OS << ".sw3" << FourCC << "$B";
break;
case llvm::Triple::ELF:
OS << "swift3_" << LongName;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SwiftRemoteMirror/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_
# libswiftRemoteMirror.dylib should not have runtime dependencies; it's
# always built as a shared library.
if(SWIFT_BUILD_DYNAMIC_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
add_swift_library(swiftRemoteMirror SHARED TARGET_LIBRARY DONT_EMBED_BITCODE
add_swift_library(swiftRemoteMirror SHARED TARGET_LIBRARY DONT_EMBED_BITCODE NOSWIFTRT
SwiftRemoteMirror.cpp
LINK_LIBRARIES swiftReflection
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CXX_FLAGS}
Expand Down
22 changes: 17 additions & 5 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ set(swift_runtime_sources
HeapObject.cpp
ImageInspectionMachO.cpp
ImageInspectionELF.cpp
ImageInspectionWin32.cpp
ImageInspectionCOFF.cpp
KnownMetadata.cpp
Metadata.cpp
MetadataLookup.cpp
Expand All @@ -71,6 +71,7 @@ set(LLVM_OPTIONAL_SOURCES
CygwinPort.cpp
ImageInspectionELF.cpp
StaticBinaryELF.cpp
SwiftRT-COFF.cpp
SwiftRT-ELF.cpp
${swift_runtime_sources}
${swift_runtime_objc_sources}
Expand Down Expand Up @@ -128,30 +129,41 @@ add_swift_library(swiftRuntime OBJECT_LIBRARY TARGET_LIBRARY
INSTALL_IN_COMPONENT never_install)

set(ELFISH_SDKS)
set(COFF_SDKS)
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
list(APPEND ELFISH_SDKS ${sdk})
elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
list(APPEND COFF_SDKS ${sdk})
endif()
endforeach()

add_swift_library(swiftImageRegistrationObject
add_swift_library(swiftImageRegistrationObjectELF
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
SwiftRT-ELF.cpp
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
TARGET_SDKS ${ELFISH_SDKS}
INSTALL_IN_COMPONENT none)
add_swift_library(swiftImageRegistrationObjectCOFF
OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
SwiftRT-COFF.cpp
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS}
LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS}
TARGET_SDKS ${COFF_SDKS}
INSTALL_IN_COMPONENT none)

foreach(sdk ${SWIFT_CONFIGURED_SDKS})
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")

if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF")
if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR
"${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF")
# TODO(compnerd) switch to the generator expression when cmake is upgraded
# to a version which supports it.
# set(swiftrtObject "$<TARGET_OBJECTS:swiftImageRegistrationObject-${arch_suffix}>")
set(swiftrtObject ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/swiftImageRegistrationObject-${arch_suffix}.dir/SwiftRT-ELF.cpp${CMAKE_C_OUTPUT_EXTENSION})
# set(swiftrtObject "$<TARGET_OBJECTS:swiftImageRegistrationObject${SWIFT_SDK_${sdk}_OBJECT_FORMAT}-${arch_suffix}>")
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})
set(swiftrtPath "${SWIFTLIB_DIR}/${arch_subdir}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}")

add_custom_command_target(swiftImageRegistration-${arch_suffix}
Expand Down
107 changes: 107 additions & 0 deletions stdlib/public/runtime/ImageInspectionCOFF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//===--- ImageInspectionWin32.cpp - Win32 image inspection ----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if !defined(__ELF__) && !defined(__MACH__)

#include "ImageInspection.h"
#include "ImageInspectionCOFF.h"

#if defined(__CYGWIN__)
#include <dlfcn.h>
#endif

using namespace swift;

namespace {
static const swift::MetadataSections *registered = nullptr;

void record(const swift::MetadataSections *sections) {
if (registered == nullptr) {
registered = sections;
sections->next = sections->prev = sections;
} else {
registered->prev->next = sections;
sections->next = registered;
sections->prev = registered->prev;
registered->prev = sections;
}
}
}

void swift::initializeProtocolConformanceLookup() {
const swift::MetadataSections *sections = registered;
while (true) {
const swift::MetadataSections::Range &conformances =
sections->swift2_protocol_conformances;
if (conformances.length)
addImageProtocolConformanceBlockCallback(reinterpret_cast<void *>(conformances.start),
conformances.length);

if (sections->next == registered)
break;
sections = sections->next;
}
}

void swift::initializeTypeMetadataRecordLookup() {
const swift::MetadataSections *sections = registered;
while (true) {
const swift::MetadataSections::Range &type_metadata =
sections->swift2_type_metadata;
if (type_metadata.length)
addImageTypeMetadataRecordBlockCallback(reinterpret_cast<void *>(type_metadata.start),
type_metadata.length);

if (sections->next == registered)
break;
sections = sections->next;
}
}

SWIFT_RUNTIME_EXPORT
void swift_addNewDSOImage(const void *addr) {
const swift::MetadataSections *sections =
static_cast<const swift::MetadataSections *>(addr);

record(sections);

const auto &protocol_conformances = sections->swift2_protocol_conformances;
const void *conformances =
reinterpret_cast<void *>(protocol_conformances.start);
if (protocol_conformances.length)
addImageProtocolConformanceBlockCallback(conformances,
protocol_conformances.length);

const auto &type_metadata = sections->swift2_type_metadata;
const void *metadata = reinterpret_cast<void *>(type_metadata.start);
if (type_metadata.length)
addImageTypeMetadataRecordBlockCallback(metadata, type_metadata.length);
}

int swift::lookupSymbol(const void *address, SymbolInfo *info) {
#if defined(__CYGWIN__)
Dl_info dlinfo;
if (dladdr(address, &dlinfo) == 0) {
return 0;
}

info->fileName = dlinfo.dli_fname;
info->baseAddress = dlinfo.dli_fbase;
info->symbolName = dli_info.dli_sname;
info->symbolAddress = dli_saddr;
return 1;
#else
return 0;
#endif // __CYGWIN__
}

#endif // !defined(__ELF__) && !defined(__MACH__)
63 changes: 63 additions & 0 deletions stdlib/public/runtime/ImageInspectionCOFF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===--- ImageInspectionELF.h -----------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// COFF specific image inspection routines.
///
//===----------------------------------------------------------------------===//

#ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H
#define SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H

#if !defined(__ELF__) && !defined(__MACH__)

#include "../SwiftShims/Visibility.h"
#include <cstdint>
#include <cstddef>

namespace swift {
struct SectionInfo {
uint64_t size;
const char *data;
};

static constexpr const uintptr_t CurrentSectionMetadataVersion = 1;

struct MetadataSections {
uintptr_t version;
uintptr_t reserved;

mutable const MetadataSections *next;
mutable const MetadataSections *prev;

struct Range {
uintptr_t start;
size_t length;
};

Range swift2_protocol_conformances;
Range swift2_type_metadata;
Range swift3_typeref;
Range swift3_reflstr;
Range swift3_fieldmd;
Range swift3_assocty;
};
} // namespace swift

// Called by injected constructors when a dynamic library is loaded.
SWIFT_RUNTIME_EXPORT
void swift_addNewDSOImage(const void *addr);

#endif // !defined(__ELF__) && !defined(__MACH__)

#endif // SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H
Loading