2828#include " swift/Runtime/Concurrent.h"
2929
3030#include < algorithm>
31- #include < atomic>
3231#include < cstdlib>
3332
3433namespace swift {
35-
36- static Lazy<ConcurrentReadableArray<swift::MetadataSections *>> registered;
37-
38- // / Adjust the \c baseAddress field of a metadata sections structure.
39- // /
40- // / \param sections A pointer to a valid \c swift::MetadataSections structure.
41- // /
42- // / This function should be called at least once before the structure or its
43- // / address is passed to code outside this file to ensure that the structure's
44- // / \c baseAddress field correctly points to the base address of the image it
45- // / is describing.
46- static void fixupMetadataSectionBaseAddress (swift::MetadataSections *sections) {
47- bool fixupNeeded = false ;
48-
49- #if defined(__ELF__)
50- // If the base address was set but the image is an ELF image, it is going to
51- // be __dso_handle which is not the value we expect (Dl_info::dli_fbase), so
52- // we need to fix it up.
53- fixupNeeded = true ;
54- #elif !defined(__MACH__)
55- // For non-ELF, non-Apple platforms, if the base address is nullptr, it
56- // implies that this image was built against an older version of the runtime
57- // that did not capture any value for the base address.
58- auto oldBaseAddress = sections->baseAddress .load (std::memory_order_relaxed);
59- if (!oldBaseAddress) {
60- fixupNeeded = true ;
61- }
62- #endif
63-
64- if (fixupNeeded) {
65- // We need to fix up the base address. We'll need a known-good address in
66- // the same image: `sections` itself will work nicely.
67- auto symbolInfo = SymbolInfo::lookup (sections);
68- if (symbolInfo.has_value () && symbolInfo->getBaseAddress ()) {
69- sections->baseAddress .store (symbolInfo->getBaseAddress (),
70- std::memory_order_relaxed);
71- }
72- }
73- }
34+ static Lazy<ConcurrentReadableArray<swift::MetadataSections *>> registered;
7435}
7536
7637SWIFT_RUNTIME_EXPORT
7738void swift_addNewDSOImage (swift::MetadataSections *sections) {
78- #if 0
79- // Ensure the base address of the sections structure is correct.
80- //
81- // Currently disabled because none of the registration functions below
82- // actually do anything with the baseAddress field. Instead,
83- // swift_enumerateAllMetadataSections() is called by other individual
84- // functions, lower in this file, that yield metadata section pointers.
85- //
86- // If one of these registration functions starts needing the baseAddress
87- // field, this call should be enabled and the calls elsewhere in the file can
88- // be removed.
89- swift::fixupMetadataSectionBaseAddress(sections);
39+ #if defined(__ELF__)
40+ if (!sections->baseAddress || sections->version <= 4 ) {
41+ // The base address was either unavailable at link time or is set to the
42+ // wrong value and will need to be recomputed. We can use the address of the
43+ // sections structure and derive the base address from there.
44+ if (auto info = swift::SymbolInfo::lookup (sections)) {
45+ sections->baseAddress = info->getBaseAddress ();
46+ }
47+ }
9048#endif
91- auto baseAddress = sections->baseAddress . load (std::memory_order_relaxed) ;
49+ auto baseAddress = sections->baseAddress ;
9250
9351 const auto &protocols_section = sections->swift5_protocols ;
9452 const void *protocols = reinterpret_cast <void *>(protocols_section.start );
@@ -144,9 +102,6 @@ void swift_enumerateAllMetadataSections(
144102) {
145103 auto snapshot = swift::registered->snapshot ();
146104 for (swift::MetadataSections *sections : snapshot) {
147- // Ensure the base address is fixed up before yielding the pointer.
148- swift::fixupMetadataSectionBaseAddress (sections);
149-
150105 // Yield the pointer and (if the callback returns false) break the loop.
151106 if (!(* body)(sections, context)) {
152107 return ;
@@ -180,11 +135,6 @@ const swift::MetadataSections *swift_getMetadataSection(size_t index) {
180135 result = snapshot[index];
181136 }
182137
183- if (result) {
184- // Ensure the base address is fixed up before returning it.
185- swift::fixupMetadataSectionBaseAddress (result);
186- }
187-
188138 return result;
189139}
190140
@@ -208,11 +158,7 @@ void swift_getMetadataSectionBaseAddress(const swift::MetadataSections *section,
208158 } else {
209159 *out_actual = nullptr ;
210160 }
211-
212- // fixupMetadataSectionBaseAddress() was already called by
213- // swift_getMetadataSection(), presumably on the same thread, so we don't need
214- // to call it again here.
215- *out_expected = section->baseAddress .load (std::memory_order_relaxed);
161+ *out_expected = section->baseAddress ;
216162}
217163
218164SWIFT_RUNTIME_EXPORT
0 commit comments