28
28
29
29
namespace swift {
30
30
31
- #ifndef NDEBUG
32
31
static swift::MetadataSections *registered = nullptr ;
33
32
34
- static void record (swift::MetadataSections *sections) {
33
+ void record (swift::MetadataSections *sections) {
35
34
if (registered == nullptr ) {
36
35
registered = sections;
37
36
sections->next = sections->prev = sections;
@@ -42,67 +41,34 @@ static void record(swift::MetadataSections *sections) {
42
41
registered->prev = sections;
43
42
}
44
43
}
45
- #endif
46
-
47
- static const void *
48
- getMetadataSectionBaseAddress (swift::MetadataSections *sections) {
49
- // If the base address was not set by the caller of swift_addNewDSOImage()
50
- // then we can assume that the caller was built against an older version of
51
- // the runtime that did not capture a value for this field. Currently nothing
52
- // is actively using the image's base address outside of tests that are built
53
- // with the runtime/stdlib, so there's no need to try to fix up the value. If
54
- // something in the runtime starts using it, we will want to either:
55
- // 1. Resolve the address from a known-good address like swift5_protocols when
56
- // the image is first loaded (in this function);
57
- // 1. Resolve the address from a known-good address like swift5_protocols when
58
- // the address is first used (and atomically swap the address back so we
59
- // don't incur the cost of lookupSymbol() each time we need it; or
60
- // 3. Introduce an ABI-breaking change so that all binaries are rebuilt and
61
- // start supplying a value for this field.
62
-
63
- #if defined(__ELF__)
64
- // If the base address was set but the image is an ELF image, it is going to
65
- // be __dso_handle which is not the value we expect (Dl_info::dli_fbase), so
66
- // we need to fix it up.
67
- if (auto baseAddress = sections->baseAddress ) {
68
- swift::SymbolInfo symbolInfo;
69
- if (lookupSymbol (baseAddress, &symbolInfo) && symbolInfo.baseAddress ) {
70
- sections->baseAddress = symbolInfo.baseAddress ;
71
- }
72
- }
73
- #endif
74
-
75
- return sections->baseAddress ;
76
- }
77
44
}
78
45
79
46
SWIFT_RUNTIME_EXPORT
80
- void swift_addNewDSOImage (swift::MetadataSections *sections) {
81
- #ifndef NDEBUG
82
- record (sections);
83
- #endif
47
+ void swift_addNewDSOImage (const void *addr) {
48
+ // We cast off the const in order to update the linked list
49
+ // data structure. This is safe to do since we don't touch
50
+ // any other fields.
51
+ swift::MetadataSections *sections =
52
+ static_cast <swift::MetadataSections *>(const_cast <void *>(addr));
84
53
85
- auto baseAddress = swift::getMetadataSectionBaseAddress (sections);
54
+ record (sections);
86
55
87
56
const auto &protocols_section = sections->swift5_protocols ;
88
57
const void *protocols = reinterpret_cast <void *>(protocols_section.start );
89
58
if (protocols_section.length )
90
- swift::addImageProtocolsBlockCallback (baseAddress,
91
- protocols, protocols_section.length );
59
+ swift::addImageProtocolsBlockCallback (protocols, protocols_section.length );
92
60
93
61
const auto &protocol_conformances = sections->swift5_protocol_conformances ;
94
62
const void *conformances =
95
63
reinterpret_cast <void *>(protocol_conformances.start );
96
64
if (protocol_conformances.length )
97
- swift::addImageProtocolConformanceBlockCallback (baseAddress, conformances,
65
+ swift::addImageProtocolConformanceBlockCallback (conformances,
98
66
protocol_conformances.length );
99
67
100
68
const auto &type_metadata = sections->swift5_type_metadata ;
101
69
const void *metadata = reinterpret_cast <void *>(type_metadata.start );
102
70
if (type_metadata.length )
103
- swift::addImageTypeMetadataRecordBlockCallback (baseAddress,
104
- metadata,
105
- type_metadata.length );
71
+ swift::addImageTypeMetadataRecordBlockCallback (metadata, type_metadata.length );
106
72
107
73
const auto &dynamic_replacements = sections->swift5_replace ;
108
74
const auto *replacements =
@@ -111,7 +77,7 @@ void swift_addNewDSOImage(swift::MetadataSections *sections) {
111
77
const auto &dynamic_replacements_some = sections->swift5_replac2 ;
112
78
const auto *replacements_some =
113
79
reinterpret_cast <void *>(dynamic_replacements_some.start );
114
- swift::addImageDynamicReplacementBlockCallback (baseAddress,
80
+ swift::addImageDynamicReplacementBlockCallback (
115
81
replacements, dynamic_replacements.length , replacements_some,
116
82
dynamic_replacements_some.length );
117
83
}
@@ -121,22 +87,70 @@ void swift_addNewDSOImage(swift::MetadataSections *sections) {
121
87
reinterpret_cast <void *>(accessible_funcs_section.start );
122
88
if (accessible_funcs_section.length )
123
89
swift::addImageAccessibleFunctionsBlockCallback (
124
- baseAddress, functions, accessible_funcs_section.length );
90
+ functions, accessible_funcs_section.length );
125
91
}
126
92
127
93
void swift::initializeProtocolLookup () {
94
+ const swift::MetadataSections *sections = registered;
95
+ while (true ) {
96
+ const swift::MetadataSectionRange &protocols =
97
+ sections->swift5_protocols ;
98
+ if (protocols.length )
99
+ addImageProtocolsBlockCallbackUnsafe (
100
+ reinterpret_cast <void *>(protocols.start ), protocols.length );
101
+
102
+ if (sections->next == registered)
103
+ break ;
104
+ sections = sections->next ;
105
+ }
128
106
}
129
107
130
108
void swift::initializeProtocolConformanceLookup () {
109
+ const swift::MetadataSections *sections = registered;
110
+ while (true ) {
111
+ const swift::MetadataSectionRange &conformances =
112
+ sections->swift5_protocol_conformances ;
113
+ if (conformances.length )
114
+ addImageProtocolConformanceBlockCallbackUnsafe (
115
+ reinterpret_cast <void *>(conformances.start ), conformances.length );
116
+
117
+ if (sections->next == registered)
118
+ break ;
119
+ sections = sections->next ;
120
+ }
131
121
}
132
122
133
123
void swift::initializeTypeMetadataRecordLookup () {
124
+ const swift::MetadataSections *sections = registered;
125
+ while (true ) {
126
+ const swift::MetadataSectionRange &type_metadata =
127
+ sections->swift5_type_metadata ;
128
+ if (type_metadata.length )
129
+ addImageTypeMetadataRecordBlockCallbackUnsafe (
130
+ reinterpret_cast <void *>(type_metadata.start ), type_metadata.length );
131
+
132
+ if (sections->next == registered)
133
+ break ;
134
+ sections = sections->next ;
135
+ }
134
136
}
135
137
136
138
void swift::initializeDynamicReplacementLookup () {
137
139
}
138
140
139
141
void swift::initializeAccessibleFunctionsLookup () {
142
+ const swift::MetadataSections *sections = registered;
143
+ while (true ) {
144
+ const swift::MetadataSectionRange &functions =
145
+ sections->swift5_accessible_functions ;
146
+ if (functions.length )
147
+ addImageAccessibleFunctionsBlockCallbackUnsafe (
148
+ reinterpret_cast <void *>(functions.start ), functions.length );
149
+
150
+ if (sections->next == registered)
151
+ break ;
152
+ sections = sections->next ;
153
+ }
140
154
}
141
155
142
156
#ifndef NDEBUG
@@ -159,35 +173,16 @@ const swift::MetadataSections *swift_getMetadataSection(size_t index) {
159
173
}
160
174
161
175
SWIFT_RUNTIME_EXPORT
162
- const char *
163
- swift_getMetadataSectionName (const swift::MetadataSections *section) {
176
+ const char *swift_getMetadataSectionName (void *metadata_section) {
164
177
swift::SymbolInfo info;
165
- if (lookupSymbol (section , &info)) {
178
+ if (lookupSymbol (metadata_section , &info)) {
166
179
if (info.fileName ) {
167
180
return info.fileName ;
168
181
}
169
182
}
170
183
return " " ;
171
184
}
172
185
173
- SWIFT_RUNTIME_EXPORT
174
- void swift_getMetadataSectionBaseAddress (const swift::MetadataSections *section,
175
- void const **out_actual,
176
- void const **out_expected) {
177
- *out_actual = nullptr ;
178
- *out_expected = section->baseAddress ;
179
-
180
- swift::SymbolInfo info;
181
- if (lookupSymbol (section, &info)) {
182
- *out_actual = info.baseAddress ;
183
- if (info.fileName ) {
184
- return info.fileName ;
185
- }
186
- }
187
- return " " ;
188
-
189
- }
190
-
191
186
SWIFT_RUNTIME_EXPORT
192
187
size_t swift_getMetadataSectionCount () {
193
188
if (swift::registered == nullptr )
0 commit comments