Skip to content

Commit 79bdd43

Browse files
authored
Merge pull request #32493 from augusto2112/elf-shim-metadata-sections
Move 'MetadataSections' from 'ImageInspectionElf.h' to SwiftShims
2 parents 1c58abf + 8c8360b commit 79bdd43

File tree

7 files changed

+77
-32
lines changed

7 files changed

+77
-32
lines changed

stdlib/public/SwiftShims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(sources
77
KeyPath.h
88
LibcOverlayShims.h
99
LibcShims.h
10+
MetadataSections.h
1011
Random.h
1112
RefCount.h
1213
RuntimeShims.h
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- MetadataSections.h -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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+
/// This file contains the declaration of the MetadataSectionsRange and
15+
/// MetadataSections struct, which represent, respectively, information about
16+
/// an image's section, and an image's metadata information (which is composed
17+
/// of multiple section information).
18+
///
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef SWIFT_STDLIB_SHIMS_METADATASECTIONS_H
22+
#define SWIFT_STDLIB_SHIMS_METADATASECTIONS_H
23+
24+
#include "SwiftStddef.h"
25+
#include "SwiftStdint.h"
26+
27+
#ifdef __cplusplus
28+
namespace swift {
29+
extern "C" {
30+
#endif
31+
32+
typedef struct MetadataSectionRange {
33+
__swift_uintptr_t start;
34+
__swift_size_t length;
35+
} MetadataSectionRange;
36+
37+
struct MetadataSections {
38+
__swift_uintptr_t version;
39+
__swift_uintptr_t reserved;
40+
41+
struct MetadataSections *next;
42+
struct MetadataSections *prev;
43+
44+
45+
MetadataSectionRange swift5_protocols;
46+
MetadataSectionRange swift5_protocol_conformances;
47+
MetadataSectionRange swift5_type_metadata;
48+
MetadataSectionRange swift5_typeref;
49+
MetadataSectionRange swift5_reflstr;
50+
MetadataSectionRange swift5_fieldmd;
51+
MetadataSectionRange swift5_assocty;
52+
MetadataSectionRange swift5_replace;
53+
MetadataSectionRange swift5_replac2;
54+
MetadataSectionRange swift5_builtin;
55+
MetadataSectionRange swift5_capture;
56+
};
57+
58+
#ifdef __cplusplus
59+
} //extern "C"
60+
} // namespace swift
61+
#endif
62+
63+
#endif // SWIFT_STDLIB_SHIMS_METADATASECTIONS_H

stdlib/public/SwiftShims/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module SwiftShims {
66
header "HeapObject.h"
77
header "KeyPath.h"
88
header "LibcShims.h"
9+
header "MetadataSections.h"
910
header "Random.h"
1011
header "RefCount.h"
1112
header "RuntimeShims.h"

stdlib/public/runtime/ImageInspection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#ifndef SWIFT_RUNTIME_IMAGEINSPECTION_H
2222
#define SWIFT_RUNTIME_IMAGEINSPECTION_H
2323

24-
#include "ImageInspectionELF.h"
2524
#include <cstdint>
2625
#include <cstddef>
2726
#if defined(__cplusplus)

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020

2121
#if defined(__ELF__)
2222

23+
#include "../SwiftShims/MetadataSections.h"
2324
#include "ImageInspection.h"
2425
#include "ImageInspectionELF.h"
2526
#include <dlfcn.h>
2627

2728
using namespace swift;
2829

2930
namespace {
30-
static const swift::MetadataSections *registered = nullptr;
31+
static swift::MetadataSections *registered = nullptr;
3132

32-
void record(const swift::MetadataSections *sections) {
33+
void record(swift::MetadataSections *sections) {
3334
if (registered == nullptr) {
3435
registered = sections;
3536
sections->next = sections->prev = sections;
@@ -45,7 +46,7 @@ void record(const swift::MetadataSections *sections) {
4546
void swift::initializeProtocolLookup() {
4647
const swift::MetadataSections *sections = registered;
4748
while (true) {
48-
const swift::MetadataSections::Range &protocols =
49+
const swift::MetadataSectionRange &protocols =
4950
sections->swift5_protocols;
5051
if (protocols.length)
5152
addImageProtocolsBlockCallbackUnsafe(
@@ -59,7 +60,7 @@ void swift::initializeProtocolLookup() {
5960
void swift::initializeProtocolConformanceLookup() {
6061
const swift::MetadataSections *sections = registered;
6162
while (true) {
62-
const swift::MetadataSections::Range &conformances =
63+
const swift::MetadataSectionRange &conformances =
6364
sections->swift5_protocol_conformances;
6465
if (conformances.length)
6566
addImageProtocolConformanceBlockCallbackUnsafe(
@@ -74,7 +75,7 @@ void swift::initializeProtocolConformanceLookup() {
7475
void swift::initializeTypeMetadataRecordLookup() {
7576
const swift::MetadataSections *sections = registered;
7677
while (true) {
77-
const swift::MetadataSections::Range &type_metadata =
78+
const swift::MetadataSectionRange &type_metadata =
7879
sections->swift5_type_metadata;
7980
if (type_metadata.length)
8081
addImageTypeMetadataRecordBlockCallbackUnsafe(
@@ -98,7 +99,11 @@ void swift_addNewDSOImage(const void *addr) {
9899
const swift::MetadataSections *sections =
99100
static_cast<const swift::MetadataSections *>(addr);
100101

101-
record(sections);
102+
// We cast off the const in order to update the linked list
103+
// data structure. This is safe to do since we don't touch
104+
// any other fields.
105+
auto casted_sections = const_cast<MetadataSections *>(sections);
106+
record(casted_sections);
102107

103108
const auto &protocols_section = sections->swift5_protocols;
104109
const void *protocols =

stdlib/public/runtime/ImageInspectionELF.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,6 @@ struct SectionInfo {
3434
};
3535

3636
static constexpr const uintptr_t CurrentSectionMetadataVersion = 1;
37-
38-
struct MetadataSections {
39-
uintptr_t version;
40-
uintptr_t reserved;
41-
42-
mutable const MetadataSections *next;
43-
mutable const MetadataSections *prev;
44-
45-
struct Range {
46-
uintptr_t start;
47-
size_t length;
48-
};
49-
50-
Range swift5_protocols;
51-
Range swift5_protocol_conformances;
52-
Range swift5_type_metadata;
53-
Range swift5_typeref;
54-
Range swift5_reflstr;
55-
Range swift5_fieldmd;
56-
Range swift5_assocty;
57-
Range swift5_replace;
58-
Range swift5_replac2;
59-
Range swift5_builtin;
60-
Range swift5_capture;
61-
};
6237
} // namespace swift
6338

6439
// Called by injected constructors when a dynamic library is loaded.

stdlib/public/runtime/SwiftRT-ELF.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ImageInspectionELF.h"
14+
#include "../SwiftShims/MetadataSections.h"
1415

1516
#include <cstddef>
1617

0 commit comments

Comments
 (0)