Skip to content

Commit ded1b6e

Browse files
committed
IRGen: get metadata symbol of class __StaticArrayStorage the correct way
Fixes an unresolved symbol error when building the stdlib on some platforms. rdar://119991570
1 parent 6f3eeb5 commit ded1b6e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/IRGen/GenConstant.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "StructLayout.h"
2828
#include "Callee.h"
2929
#include "ConstantBuilder.h"
30+
#include "DebugTypeInfo.h"
31+
#include "swift/IRGen/Linking.h"
3032
#include "swift/Basic/Range.h"
3133
#include "swift/SIL/SILModule.h"
3234
#include "llvm/Support/BLAKE3.h"
@@ -438,11 +440,12 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
438440
IGM.swiftImmortalRefCount = var;
439441
}
440442
if (!IGM.swiftStaticArrayMetadata) {
441-
// type metadata for class __StaticArrayStorage
442-
auto *var = new llvm::GlobalVariable(IGM.Module, IGM.TypeMetadataStructTy,
443-
/*constant*/ true, llvm::GlobalValue::ExternalLinkage,
444-
/*initializer*/ nullptr, "$ss20__StaticArrayStorageCN");
445-
IGM.swiftStaticArrayMetadata = var;
443+
auto *classDecl = IGM.getStaticArrayStorageDecl();
444+
assert(classDecl && "no __StaticArrayStorage in stdlib");
445+
CanType classTy = CanType(ClassType::get(classDecl, Type(), IGM.Context));
446+
LinkEntity entity = LinkEntity::forTypeMetadata(classTy, TypeMetadataAddress::AddressPoint);
447+
auto *metatype = IGM.getAddrOfLLVMVariable(entity, NotForDefinition, DebugTypeInfo());
448+
IGM.swiftStaticArrayMetadata = cast<llvm::GlobalVariable>(metatype);
446449
}
447450
elements[0].add(llvm::ConstantStruct::get(ObjectHeaderTy, {
448451
IGM.swiftStaticArrayMetadata,

lib/IRGen/IRGenModule.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -2046,8 +2046,23 @@ bool IRGenModule::canMakeStaticObjectsReadOnly() {
20462046
if (!Triple.isOSDarwin())
20472047
return false;
20482048

2049-
return getAvailabilityContext().isContainedIn(
2050-
Context.getStaticReadOnlyArraysAvailability());
2049+
if (!getAvailabilityContext().isContainedIn(Context.getStaticReadOnlyArraysAvailability()))
2050+
return false;
2051+
2052+
if (!getStaticArrayStorageDecl())
2053+
return false;
2054+
2055+
return true;
2056+
}
2057+
2058+
ClassDecl *IRGenModule::getStaticArrayStorageDecl() {
2059+
SmallVector<ValueDecl *, 1> results;
2060+
Context.lookupInSwiftModule("__StaticArrayStorage", results);
2061+
2062+
if (results.size() != 1)
2063+
return nullptr;
2064+
2065+
return dyn_cast<ClassDecl>(results[0]);
20512066
}
20522067

20532068
void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {

lib/IRGen/IRGenModule.h

+2
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ class IRGenModule {
927927

928928
bool canMakeStaticObjectsReadOnly();
929929

930+
ClassDecl *getStaticArrayStorageDecl();
931+
930932
bool canUseObjCSymbolicReferences();
931933

932934
Size getAtomicBoolSize() const { return AtomicBoolSize; }

0 commit comments

Comments
 (0)