From 8c8360b660deba3b650257be76fd461644825273 Mon Sep 17 00:00:00 2001 From: Augusto Noronha Date: Mon, 22 Jun 2020 09:17:35 -0300 Subject: [PATCH] Move 'MetadataSections' from 'ImageInspectionElf.h' to SwiftShims --- stdlib/public/SwiftShims/CMakeLists.txt | 1 + stdlib/public/SwiftShims/MetadataSections.h | 63 ++++++++++++++++++++ stdlib/public/SwiftShims/module.modulemap | 1 + stdlib/public/runtime/ImageInspection.h | 1 - stdlib/public/runtime/ImageInspectionELF.cpp | 17 ++++-- stdlib/public/runtime/ImageInspectionELF.h | 25 -------- stdlib/public/runtime/SwiftRT-ELF.cpp | 1 + 7 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 stdlib/public/SwiftShims/MetadataSections.h diff --git a/stdlib/public/SwiftShims/CMakeLists.txt b/stdlib/public/SwiftShims/CMakeLists.txt index b6d3edc47818c..c9a4aac64f3f5 100644 --- a/stdlib/public/SwiftShims/CMakeLists.txt +++ b/stdlib/public/SwiftShims/CMakeLists.txt @@ -7,6 +7,7 @@ set(sources KeyPath.h LibcOverlayShims.h LibcShims.h + MetadataSections.h Random.h RefCount.h RuntimeShims.h diff --git a/stdlib/public/SwiftShims/MetadataSections.h b/stdlib/public/SwiftShims/MetadataSections.h new file mode 100644 index 0000000000000..a45383e8c465a --- /dev/null +++ b/stdlib/public/SwiftShims/MetadataSections.h @@ -0,0 +1,63 @@ +//===--- MetadataSections.h -----------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2020 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 +/// This file contains the declaration of the MetadataSectionsRange and +/// MetadataSections struct, which represent, respectively, information about +/// an image's section, and an image's metadata information (which is composed +/// of multiple section information). +/// +//===----------------------------------------------------------------------===// + +#ifndef SWIFT_STDLIB_SHIMS_METADATASECTIONS_H +#define SWIFT_STDLIB_SHIMS_METADATASECTIONS_H + +#include "SwiftStddef.h" +#include "SwiftStdint.h" + +#ifdef __cplusplus +namespace swift { +extern "C" { +#endif + +typedef struct MetadataSectionRange { + __swift_uintptr_t start; + __swift_size_t length; +} MetadataSectionRange; + +struct MetadataSections { + __swift_uintptr_t version; + __swift_uintptr_t reserved; + + struct MetadataSections *next; + struct MetadataSections *prev; + + + MetadataSectionRange swift5_protocols; + MetadataSectionRange swift5_protocol_conformances; + MetadataSectionRange swift5_type_metadata; + MetadataSectionRange swift5_typeref; + MetadataSectionRange swift5_reflstr; + MetadataSectionRange swift5_fieldmd; + MetadataSectionRange swift5_assocty; + MetadataSectionRange swift5_replace; + MetadataSectionRange swift5_replac2; + MetadataSectionRange swift5_builtin; + MetadataSectionRange swift5_capture; +}; + +#ifdef __cplusplus +} //extern "C" +} // namespace swift +#endif + +#endif // SWIFT_STDLIB_SHIMS_METADATASECTIONS_H diff --git a/stdlib/public/SwiftShims/module.modulemap b/stdlib/public/SwiftShims/module.modulemap index 493e0db7df559..f855d10e973ca 100644 --- a/stdlib/public/SwiftShims/module.modulemap +++ b/stdlib/public/SwiftShims/module.modulemap @@ -6,6 +6,7 @@ module SwiftShims { header "HeapObject.h" header "KeyPath.h" header "LibcShims.h" + header "MetadataSections.h" header "Random.h" header "RefCount.h" header "RuntimeShims.h" diff --git a/stdlib/public/runtime/ImageInspection.h b/stdlib/public/runtime/ImageInspection.h index 32e3b2122ce40..8a20756947153 100644 --- a/stdlib/public/runtime/ImageInspection.h +++ b/stdlib/public/runtime/ImageInspection.h @@ -21,7 +21,6 @@ #ifndef SWIFT_RUNTIME_IMAGEINSPECTION_H #define SWIFT_RUNTIME_IMAGEINSPECTION_H -#include "ImageInspectionELF.h" #include #include #if defined(__cplusplus) diff --git a/stdlib/public/runtime/ImageInspectionELF.cpp b/stdlib/public/runtime/ImageInspectionELF.cpp index 331ee3614f54f..d5a245be4fae7 100644 --- a/stdlib/public/runtime/ImageInspectionELF.cpp +++ b/stdlib/public/runtime/ImageInspectionELF.cpp @@ -20,6 +20,7 @@ #if defined(__ELF__) +#include "../SwiftShims/MetadataSections.h" #include "ImageInspection.h" #include "ImageInspectionELF.h" #include @@ -27,9 +28,9 @@ using namespace swift; namespace { -static const swift::MetadataSections *registered = nullptr; +static swift::MetadataSections *registered = nullptr; -void record(const swift::MetadataSections *sections) { +void record(swift::MetadataSections *sections) { if (registered == nullptr) { registered = sections; sections->next = sections->prev = sections; @@ -45,7 +46,7 @@ void record(const swift::MetadataSections *sections) { void swift::initializeProtocolLookup() { const swift::MetadataSections *sections = registered; while (true) { - const swift::MetadataSections::Range &protocols = + const swift::MetadataSectionRange &protocols = sections->swift5_protocols; if (protocols.length) addImageProtocolsBlockCallbackUnsafe( @@ -59,7 +60,7 @@ void swift::initializeProtocolLookup() { void swift::initializeProtocolConformanceLookup() { const swift::MetadataSections *sections = registered; while (true) { - const swift::MetadataSections::Range &conformances = + const swift::MetadataSectionRange &conformances = sections->swift5_protocol_conformances; if (conformances.length) addImageProtocolConformanceBlockCallbackUnsafe( @@ -74,7 +75,7 @@ void swift::initializeProtocolConformanceLookup() { void swift::initializeTypeMetadataRecordLookup() { const swift::MetadataSections *sections = registered; while (true) { - const swift::MetadataSections::Range &type_metadata = + const swift::MetadataSectionRange &type_metadata = sections->swift5_type_metadata; if (type_metadata.length) addImageTypeMetadataRecordBlockCallbackUnsafe( @@ -98,7 +99,11 @@ void swift_addNewDSOImage(const void *addr) { const swift::MetadataSections *sections = static_cast(addr); - record(sections); + // We cast off the const in order to update the linked list + // data structure. This is safe to do since we don't touch + // any other fields. + auto casted_sections = const_cast(sections); + record(casted_sections); const auto &protocols_section = sections->swift5_protocols; const void *protocols = diff --git a/stdlib/public/runtime/ImageInspectionELF.h b/stdlib/public/runtime/ImageInspectionELF.h index afa2ee7150603..1f227358eaad8 100644 --- a/stdlib/public/runtime/ImageInspectionELF.h +++ b/stdlib/public/runtime/ImageInspectionELF.h @@ -34,31 +34,6 @@ struct SectionInfo { }; 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 swift5_protocols; - Range swift5_protocol_conformances; - Range swift5_type_metadata; - Range swift5_typeref; - Range swift5_reflstr; - Range swift5_fieldmd; - Range swift5_assocty; - Range swift5_replace; - Range swift5_replac2; - Range swift5_builtin; - Range swift5_capture; -}; } // namespace swift // Called by injected constructors when a dynamic library is loaded. diff --git a/stdlib/public/runtime/SwiftRT-ELF.cpp b/stdlib/public/runtime/SwiftRT-ELF.cpp index fa585a8a68241..93d6e6658f6ff 100644 --- a/stdlib/public/runtime/SwiftRT-ELF.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "ImageInspectionELF.h" +#include "../SwiftShims/MetadataSections.h" #include