Skip to content

Make Objective-C protocol metadata weak hidden #40486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 87 additions & 70 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,22 +1319,25 @@ namespace {
assert(TheExtension && "can't emit category data for a class");
ConstantInitBuilder builder(IGM);
auto fields = builder.beginStruct();
auto internalLinkage = llvm::GlobalVariable::InternalLinkage;

// struct category_t {
// char const *name;
fields.add(IGM.getAddrOfGlobalString(CategoryName));
// const class_t *theClass;
fields.add(getClassMetadataRef());
// const method_list_t *instanceMethods;
emitAndAddMethodList(fields, MethodListKind::InstanceMethods);
emitAndAddMethodList(fields, MethodListKind::InstanceMethods,
internalLinkage);
// const method_list_t *classMethods;
emitAndAddMethodList(fields, MethodListKind::ClassMethods);
emitAndAddMethodList(fields, MethodListKind::ClassMethods,
internalLinkage);
// const protocol_list_t *baseProtocols;
fields.add(buildProtocolList());
fields.add(buildProtocolList(internalLinkage));
// const property_list_t *properties;
fields.add(buildPropertyList(ForClass));
fields.add(buildPropertyList(ForClass, internalLinkage));
// const property_list_t *classProperties;
fields.add(buildPropertyList(ForMetaClass));
fields.add(buildPropertyList(ForMetaClass, internalLinkage));
// uint32_t size;
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
// structures.
Expand All @@ -1343,13 +1346,15 @@ namespace {
// };

assert(fields.getNextOffsetFromGlobal() == size);
return buildGlobalVariable(fields, "_CATEGORY_", /*const*/ true);
return buildGlobalVariable(fields, "_CATEGORY_", /*const*/ true,
internalLinkage);
}

llvm::Constant *emitProtocol() {
ConstantInitBuilder builder(IGM);
auto fields = builder.beginStruct();
llvm::SmallString<64> nameBuffer;
auto weakLinkage = llvm::GlobalVariable::WeakAnyLinkage;

assert(isBuildingProtocol() && "not emitting a protocol");

Expand All @@ -1359,17 +1364,20 @@ namespace {
// char const *name;
fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer)));
// const protocol_list_t *baseProtocols;
fields.add(buildProtocolList());
fields.add(buildProtocolList(weakLinkage));
// const method_list_t *requiredInstanceMethods;
emitAndAddMethodList(fields, MethodListKind::InstanceMethods);
emitAndAddMethodList(fields, MethodListKind::InstanceMethods,
weakLinkage);
// const method_list_t *requiredClassMethods;
emitAndAddMethodList(fields, MethodListKind::ClassMethods);
emitAndAddMethodList(fields, MethodListKind::ClassMethods, weakLinkage);
// const method_list_t *optionalInstanceMethods;
emitAndAddMethodList(fields, MethodListKind::OptionalInstanceMethods);
emitAndAddMethodList(fields, MethodListKind::OptionalInstanceMethods,
weakLinkage);
// const method_list_t *optionalClassMethods;
emitAndAddMethodList(fields, MethodListKind::OptionalClassMethods);
emitAndAddMethodList(fields, MethodListKind::OptionalClassMethods,
weakLinkage);
// const property_list_t *properties;
fields.add(buildPropertyList(ForClass));
fields.add(buildPropertyList(ForClass, weakLinkage));

// uint32_t size;
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
Expand All @@ -1391,11 +1399,12 @@ namespace {
// const char *demangledName;
fields.addNullPointer(IGM.Int8PtrTy);
// const property_list_t *classProperties;
fields.add(buildPropertyList(ForMetaClass));
fields.add(buildPropertyList(ForMetaClass, weakLinkage));
// };

assert(fields.getNextOffsetFromGlobal() == size);
return buildGlobalVariable(fields, "_PROTOCOL_", /*const*/ true);
return buildGlobalVariable(fields, "_PROTOCOL_", /*const*/ true,
weakLinkage);
}

void emitRODataFields(ConstantStructBuilder &b,
Expand Down Expand Up @@ -1456,12 +1465,14 @@ namespace {
b.add(buildName());

// const method_list_t *baseMethods;
emitAndAddMethodList(b, forMeta ? MethodListKind::ClassMethods
: MethodListKind::InstanceMethods);
emitAndAddMethodList(b,
forMeta ? MethodListKind::ClassMethods
: MethodListKind::InstanceMethods,
llvm::GlobalVariable::InternalLinkage);

// const protocol_list_t *baseProtocols;
// Apparently, this list is the same in the class and the metaclass.
b.add(buildProtocolList());
b.add(buildProtocolList(llvm::GlobalVariable::InternalLinkage));

// const ivar_list_t *ivars;
if (forMeta) {
Expand All @@ -1475,7 +1486,7 @@ namespace {
b.addNullPointer(IGM.Int8PtrTy);

// const property_list_t *baseProperties;
b.add(buildPropertyList(forMeta));
b.add(buildPropertyList(forMeta, llvm::GlobalVariable::InternalLinkage));

// If hasUpdater is true, the metadata update callback goes here.
if (hasUpdater) {
Expand Down Expand Up @@ -1503,7 +1514,8 @@ namespace {
// statically. Otherwise, the ObjC runtime may slide the InstanceSize
// based on changing base class layout.
return buildGlobalVariable(fields, dataSuffix,
/*const*/ forMeta || FieldLayout->isFixedSize());
/*const*/ forMeta || FieldLayout->isFixedSize(),
llvm::GlobalVariable::InternalLinkage);
}

private:
Expand Down Expand Up @@ -1723,7 +1735,8 @@ namespace {

/// Emit the method list and add the pointer to the `builder`.
void emitAndAddMethodList(ConstantInitBuilder::StructBuilder &builder,
MethodListKind kind) {
MethodListKind kind,
llvm::GlobalValue::LinkageTypes linkage) {
ArrayRef<MethodDescriptor> methods;
StringRef namePrefix;
switch (kind) {
Expand All @@ -1748,7 +1761,8 @@ namespace {
namePrefix = "_PROTOCOL_INSTANCE_METHODS_OPT_";
break;
}
llvm::Constant *methodListPtr = buildMethodList(methods, namePrefix);
llvm::Constant *methodListPtr =
buildMethodList(methods, namePrefix, linkage);
builder.add(methodListPtr);
}

Expand All @@ -1768,12 +1782,9 @@ namespace {
return null();
}

auto *gv_as_const = buildGlobalVariable(array, "_PROTOCOL_METHOD_TYPES_",
/*const*/ true,
/*likage*/ llvm::GlobalVariable::WeakAnyLinkage);
llvm::GlobalValue *gv = (llvm::GlobalValue *)gv_as_const;
gv->setVisibility(llvm::GlobalValue::HiddenVisibility);
return gv;
return buildGlobalVariable(array, "_PROTOCOL_METHOD_TYPES_",
/*const*/ true,
llvm::GlobalVariable::WeakAnyLinkage);
}

void buildExtMethodTypes(ConstantArrayBuilder &array,
Expand All @@ -1796,13 +1807,14 @@ namespace {
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildMethodList(ArrayRef<MethodDescriptor> methods,
StringRef name) {
return buildOptionalList(methods, 3 * IGM.getPointerSize(), name,
/*isConst*/ false,
[&](ConstantArrayBuilder &descriptors,
MethodDescriptor descriptor) {
buildMethod(descriptors, descriptor);
});
StringRef name,
llvm::GlobalValue::LinkageTypes linkage) {
return buildOptionalList(
methods, 3 * IGM.getPointerSize(), name,
/*isConst*/ false, linkage,
[&](ConstantArrayBuilder &descriptors, MethodDescriptor descriptor) {
buildMethod(descriptors, descriptor);
});
}

/*** Protocols *********************************************************/
Expand All @@ -1819,16 +1831,15 @@ namespace {
/// };
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildProtocolList() {
return buildOptionalList(Protocols, Size(0),
chooseNamePrefix("_PROTOCOLS_",
"_CATEGORY_PROTOCOLS_",
"_PROTOCOL_PROTOCOLS_"),
/*isConst*/ true,
[&](ConstantArrayBuilder &descriptors,
ProtocolDecl *protocol) {
buildProtocol(descriptors, protocol);
});
llvm::Constant *buildProtocolList(llvm::GlobalValue::LinkageTypes linkage) {
return buildOptionalList(
Protocols, Size(0),
chooseNamePrefix("_PROTOCOLS_", "_CATEGORY_PROTOCOLS_",
"_PROTOCOL_PROTOCOLS_"),
/*isConst*/ true, linkage,
[&](ConstantArrayBuilder &descriptors, ProtocolDecl *protocol) {
buildProtocol(descriptors, protocol);
});
}

void buildProtocol(ConstantArrayBuilder &array, ProtocolDecl *protocol) {
Expand Down Expand Up @@ -1949,12 +1960,12 @@ namespace {
/// This method does not return a value of a predictable type.
llvm::Constant *buildIvarList() {
Size eltSize = 3 * IGM.getPointerSize() + Size(8);
return buildOptionalList(Ivars, eltSize, "_IVARS_",
/*constant*/ true,
[&](ConstantArrayBuilder &descriptors,
Field field) {
buildIvar(descriptors, field);
});
return buildOptionalList(
Ivars, eltSize, "_IVARS_",
/*constant*/ true, llvm::GlobalVariable::InternalLinkage,
[&](ConstantArrayBuilder &descriptors, Field field) {
buildIvar(descriptors, field);
});
}

/*** Properties ********************************************************/
Expand Down Expand Up @@ -2062,12 +2073,14 @@ namespace {
/// };
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildPropertyList(ForMetaClass_t classOrMeta) {
llvm::Constant *buildPropertyList(ForMetaClass_t classOrMeta,
llvm::GlobalValue::LinkageTypes linkage) {
if (classOrMeta == ForClass) {
return buildPropertyList(InstanceProperties,
chooseNamePrefix("_PROPERTIES_",
"_CATEGORY_PROPERTIES_",
"_PROTOCOL_PROPERTIES_"));
"_PROTOCOL_PROPERTIES_"),
linkage);
}

// Older OSs' libobjcs can't handle class property data.
Expand All @@ -2079,18 +2092,20 @@ namespace {
return buildPropertyList(ClassProperties,
chooseNamePrefix("_CLASS_PROPERTIES_",
"_CATEGORY_CLASS_PROPERTIES_",
"_PROTOCOL_CLASS_PROPERTIES_"));
"_PROTOCOL_CLASS_PROPERTIES_"),
linkage);
}

llvm::Constant *buildPropertyList(ArrayRef<VarDecl*> properties,
StringRef namePrefix) {
llvm::Constant *buildPropertyList(ArrayRef<VarDecl *> properties,
StringRef namePrefix,
llvm::GlobalValue::LinkageTypes linkage) {
Size eltSize = 2 * IGM.getPointerSize();
return buildOptionalList(properties, eltSize, namePrefix,
/*constant*/ true,
[&](ConstantArrayBuilder &descriptors,
VarDecl *property) {
buildProperty(descriptors, property);
});
return buildOptionalList(
properties, eltSize, namePrefix,
/*constant*/ true, linkage,
[&](ConstantArrayBuilder &descriptors, VarDecl *property) {
buildProperty(descriptors, property);
});
}

/*** General ***********************************************************/
Expand All @@ -2102,10 +2117,9 @@ namespace {
/// \param optionalEltSize - if non-zero, a size which needs
/// to be placed in the list header
template <class C, class Fn>
llvm::Constant *buildOptionalList(const C &objects,
Size optionalEltSize,
StringRef nameBase,
bool isConst,
llvm::Constant *buildOptionalList(const C &objects, Size optionalEltSize,
StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage,
Fn &&buildElement) {
if (objects.empty())
return null();
Expand Down Expand Up @@ -2144,9 +2158,9 @@ namespace {

fields.fillPlaceholderWithInt(countPosition, countType, count);

return buildGlobalVariable(fields, nameBase, isConst);
return buildGlobalVariable(fields, nameBase, isConst, linkage);
}

/// Get the name of the class or protocol to mangle into the ObjC symbol
/// name.
StringRef getEntityName(llvm::SmallVectorImpl<char> &buffer) const {
Expand All @@ -2173,9 +2187,9 @@ namespace {
/// Build a private global variable as a structure containing the
/// given fields.
template <class B>
llvm::Constant *buildGlobalVariable(B &fields, StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage =
llvm::GlobalVariable::InternalLinkage) {
llvm::Constant *
buildGlobalVariable(B &fields, StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage) {
llvm::SmallString<64> nameBuffer;
auto var =
fields.finishAndCreateGlobal(Twine(nameBase)
Expand All @@ -2186,6 +2200,9 @@ namespace {
IGM.getPointerAlignment(),
/*constant*/ true,
linkage);
if (linkage == llvm::GlobalVariable::WeakAnyLinkage) {
var->setVisibility(llvm::GlobalValue::HiddenVisibility);
}

switch (IGM.TargetInfo.OutputObjectFormat) {
case llvm::Triple::MachO:
Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/objc_async_protocol_irgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let anyObject: AnyObject = (MyAsyncProtocol.self as AnyObject) // or something l
// Make sure we don't emit 2 copies of methods, due to a completion-handler
// version and another due to an async based version.

// CHECK-LABEL: @_PROTOCOL_INSTANCE_METHODS_MyAsyncProtocol = internal constant
// CHECK-LABEL: @_PROTOCOL_INSTANCE_METHODS_MyAsyncProtocol = weak hidden constant
// CHECK-SAME: selector_data(myAsyncMethod:)
// CHECK-NOT: selector_data(myAsyncMethod:)
// CHECK-SAME: align [[ALIGNMENT]]
6 changes: 3 additions & 3 deletions test/IRGen/generic_casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import Foundation
import gizmo

// -- Protocol records for cast-to ObjC protocols
// CHECK: @_PROTOCOL__TtP13generic_casts10ObjCProto1_ = internal constant
// CHECK: @_PROTOCOL__TtP13generic_casts10ObjCProto1_ = weak hidden constant
// CHECK: @"\01l_OBJC_LABEL_PROTOCOL_$__TtP13generic_casts10ObjCProto1_" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL__TtP13generic_casts10ObjCProto1_ to i8*), section {{"__DATA,__objc_protolist,coalesced,no_dead_strip"|"objc_protolist"|".objc_protolist\$B"}}
// CHECK: @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP13generic_casts10ObjCProto1_" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL__TtP13generic_casts10ObjCProto1_ to i8*), section {{"__DATA,__objc_protorefs,coalesced,no_dead_strip"|"objc_protorefs"|".objc_protorefs\$B"}}

// CHECK: @_PROTOCOL_NSRuncing = internal constant
// CHECK: @_PROTOCOL_NSRuncing = weak hidden constant
// CHECK: @"\01l_OBJC_LABEL_PROTOCOL_$_NSRuncing" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL_NSRuncing to i8*), section {{"__DATA,__objc_protolist,coalesced,no_dead_strip"|"objc_protolist"|".objc_protolist\$B"}}
// CHECK: @"\01l_OBJC_PROTOCOL_REFERENCE_$_NSRuncing" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL_NSRuncing to i8*), section {{"__DATA,__objc_protorefs,coalesced,no_dead_strip"|"objc_protorefs"|".objc_protorefs\$B"}}

Expand All @@ -23,7 +23,7 @@ import gizmo

// CHECK: @_DATA__TtC13generic_casts10ObjCClass2 = internal constant {{.*}} @_PROTOCOLS__TtC13generic_casts10ObjCClass2

// CHECK: @_PROTOCOL_PROTOCOLS__TtP13generic_casts10ObjCProto2_ = internal constant { i64, [1 x i8*] } {
// CHECK: @_PROTOCOL_PROTOCOLS__TtP13generic_casts10ObjCProto2_ = weak hidden constant { i64, [1 x i8*] } {
// CHECK: i64 1,
// CHECK: @_PROTOCOL__TtP13generic_casts10ObjCProto1_
// CHECK: }
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/objc_async_metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MyClass: NSObject {
}

// CHECK: [[ENCODE_ASYNC_STRING_PROTO:@.*]] = private unnamed_addr constant [15 x i8] c"v32@0:8@16@?24\00"
// CHECK-LABEL: @_PROTOCOL_INSTANCE_METHODS__TtP19objc_async_metadata7MyProto_ = internal constant
// CHECK-LABEL: @_PROTOCOL_INSTANCE_METHODS__TtP19objc_async_metadata7MyProto_ = weak hidden constant
// CHECK-SAME: _selector_data(myProtoRequirement:completionHandler:)
// CHECK-SAME: [15 x i8]* [[ENCODE_ASYNC_STRING_PROTO]]

Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/objc_properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class SomeWrapperTests {
// CHECK: i8* getelementptr inbounds ([11 x i8], [11 x i8]* [[SETTER_SIGNATURE]], i64 0, i64 0),
// CHECK: @"$s15objc_properties4TreeC6parentACSgvsTo{{(.ptrauth)?}}"

// CHECK: @_PROTOCOL__TtP15objc_properties5Proto_ = internal constant { {{.+}} } {
// CHECK: @_PROTOCOL__TtP15objc_properties5Proto_ = weak hidden constant { {{.+}} } {
// CHECK: i8* null,
// CHECK: i8* getelementptr inbounds ([{{.+}} x i8], [{{.+}} x i8]* {{@.+}}, i64 0, i64 0),
// CHECK: i8* null,
Expand All @@ -289,7 +289,7 @@ class SomeWrapperTests {
// CHECK: [[PROTOCOLPROPERTY_NAME:@.+]] = private unnamed_addr constant [6 x i8] c"value\00"
// CHECK: [[PROTOCOLPROPERTY_ATTRS:@.+]] = private unnamed_addr constant [7 x i8] c"Tq,N,R\00"

// CHECK: @_PROTOCOL_PROPERTIES__TtP15objc_properties5Proto_ = internal constant { {{.*}}] } {
// CHECK: @_PROTOCOL_PROPERTIES__TtP15objc_properties5Proto_ = weak hidden constant { {{.*}}] } {
// CHECK: i32 16,
// CHECK: i32 1,
// CHECK: [1 x { i8*, i8* }] [{
Expand All @@ -301,7 +301,7 @@ class SomeWrapperTests {
// CHECK-NEW: [[PROTOCOLCLASSPROPERTY_NAME:@.+]] = private unnamed_addr constant [15 x i8] c"sharedInstance\00"
// CHECK-NEW: [[PROTOCOLCLASSPROPERTY_ATTRS:@.+]] = private unnamed_addr constant [7 x i8] c"T@,N,&\00"

// CHECK-NEW: @_PROTOCOL_CLASS_PROPERTIES__TtP15objc_properties5Proto_ = internal constant { {{.*}}] } {
// CHECK-NEW: @_PROTOCOL_CLASS_PROPERTIES__TtP15objc_properties5Proto_ = weak hidden constant { {{.*}}] } {
// CHECK-NEW: i32 16,
// CHECK-NEW: i32 1,
// CHECK-NEW: [1 x { i8*, i8* }] [{
Expand Down
Loading