@@ -1319,22 +1319,25 @@ namespace {
1319
1319
assert (TheExtension && " can't emit category data for a class" );
1320
1320
ConstantInitBuilder builder (IGM);
1321
1321
auto fields = builder.beginStruct ();
1322
+ auto internalLinkage = llvm::GlobalVariable::InternalLinkage;
1322
1323
1323
1324
// struct category_t {
1324
1325
// char const *name;
1325
1326
fields.add (IGM.getAddrOfGlobalString (CategoryName));
1326
1327
// const class_t *theClass;
1327
1328
fields.add (getClassMetadataRef ());
1328
1329
// const method_list_t *instanceMethods;
1329
- emitAndAddMethodList (fields, MethodListKind::InstanceMethods);
1330
+ emitAndAddMethodList (fields, MethodListKind::InstanceMethods,
1331
+ internalLinkage);
1330
1332
// const method_list_t *classMethods;
1331
- emitAndAddMethodList (fields, MethodListKind::ClassMethods);
1333
+ emitAndAddMethodList (fields, MethodListKind::ClassMethods,
1334
+ internalLinkage);
1332
1335
// const protocol_list_t *baseProtocols;
1333
- fields.add (buildProtocolList ());
1336
+ fields.add (buildProtocolList (internalLinkage ));
1334
1337
// const property_list_t *properties;
1335
- fields.add (buildPropertyList (ForClass));
1338
+ fields.add (buildPropertyList (ForClass, internalLinkage ));
1336
1339
// const property_list_t *classProperties;
1337
- fields.add (buildPropertyList (ForMetaClass));
1340
+ fields.add (buildPropertyList (ForMetaClass, internalLinkage ));
1338
1341
// uint32_t size;
1339
1342
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
1340
1343
// structures.
@@ -1343,13 +1346,15 @@ namespace {
1343
1346
// };
1344
1347
1345
1348
assert (fields.getNextOffsetFromGlobal () == size);
1346
- return buildGlobalVariable (fields, " _CATEGORY_" , /* const*/ true );
1349
+ return buildGlobalVariable (fields, " _CATEGORY_" , /* const*/ true ,
1350
+ internalLinkage);
1347
1351
}
1348
1352
1349
1353
llvm::Constant *emitProtocol () {
1350
1354
ConstantInitBuilder builder (IGM);
1351
1355
auto fields = builder.beginStruct ();
1352
1356
llvm::SmallString<64 > nameBuffer;
1357
+ auto weakLinkage = llvm::GlobalVariable::WeakAnyLinkage;
1353
1358
1354
1359
assert (isBuildingProtocol () && " not emitting a protocol" );
1355
1360
@@ -1359,17 +1364,20 @@ namespace {
1359
1364
// char const *name;
1360
1365
fields.add (IGM.getAddrOfGlobalString (getEntityName (nameBuffer)));
1361
1366
// const protocol_list_t *baseProtocols;
1362
- fields.add (buildProtocolList ());
1367
+ fields.add (buildProtocolList (weakLinkage ));
1363
1368
// const method_list_t *requiredInstanceMethods;
1364
- emitAndAddMethodList (fields, MethodListKind::InstanceMethods);
1369
+ emitAndAddMethodList (fields, MethodListKind::InstanceMethods,
1370
+ weakLinkage);
1365
1371
// const method_list_t *requiredClassMethods;
1366
- emitAndAddMethodList (fields, MethodListKind::ClassMethods);
1372
+ emitAndAddMethodList (fields, MethodListKind::ClassMethods, weakLinkage );
1367
1373
// const method_list_t *optionalInstanceMethods;
1368
- emitAndAddMethodList (fields, MethodListKind::OptionalInstanceMethods);
1374
+ emitAndAddMethodList (fields, MethodListKind::OptionalInstanceMethods,
1375
+ weakLinkage);
1369
1376
// const method_list_t *optionalClassMethods;
1370
- emitAndAddMethodList (fields, MethodListKind::OptionalClassMethods);
1377
+ emitAndAddMethodList (fields, MethodListKind::OptionalClassMethods,
1378
+ weakLinkage);
1371
1379
// const property_list_t *properties;
1372
- fields.add (buildPropertyList (ForClass));
1380
+ fields.add (buildPropertyList (ForClass, weakLinkage ));
1373
1381
1374
1382
// uint32_t size;
1375
1383
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
@@ -1391,11 +1399,12 @@ namespace {
1391
1399
// const char *demangledName;
1392
1400
fields.addNullPointer (IGM.Int8PtrTy );
1393
1401
// const property_list_t *classProperties;
1394
- fields.add (buildPropertyList (ForMetaClass));
1402
+ fields.add (buildPropertyList (ForMetaClass, weakLinkage ));
1395
1403
// };
1396
1404
1397
1405
assert (fields.getNextOffsetFromGlobal () == size);
1398
- return buildGlobalVariable (fields, " _PROTOCOL_" , /* const*/ true );
1406
+ return buildGlobalVariable (fields, " _PROTOCOL_" , /* const*/ true ,
1407
+ weakLinkage);
1399
1408
}
1400
1409
1401
1410
void emitRODataFields (ConstantStructBuilder &b,
@@ -1456,12 +1465,14 @@ namespace {
1456
1465
b.add (buildName ());
1457
1466
1458
1467
// const method_list_t *baseMethods;
1459
- emitAndAddMethodList (b, forMeta ? MethodListKind::ClassMethods
1460
- : MethodListKind::InstanceMethods);
1468
+ emitAndAddMethodList (b,
1469
+ forMeta ? MethodListKind::ClassMethods
1470
+ : MethodListKind::InstanceMethods,
1471
+ llvm::GlobalVariable::InternalLinkage);
1461
1472
1462
1473
// const protocol_list_t *baseProtocols;
1463
1474
// Apparently, this list is the same in the class and the metaclass.
1464
- b.add (buildProtocolList ());
1475
+ b.add (buildProtocolList (llvm::GlobalVariable::InternalLinkage ));
1465
1476
1466
1477
// const ivar_list_t *ivars;
1467
1478
if (forMeta) {
@@ -1475,7 +1486,7 @@ namespace {
1475
1486
b.addNullPointer (IGM.Int8PtrTy );
1476
1487
1477
1488
// const property_list_t *baseProperties;
1478
- b.add (buildPropertyList (forMeta));
1489
+ b.add (buildPropertyList (forMeta, llvm::GlobalVariable::InternalLinkage ));
1479
1490
1480
1491
// If hasUpdater is true, the metadata update callback goes here.
1481
1492
if (hasUpdater) {
@@ -1503,7 +1514,8 @@ namespace {
1503
1514
// statically. Otherwise, the ObjC runtime may slide the InstanceSize
1504
1515
// based on changing base class layout.
1505
1516
return buildGlobalVariable (fields, dataSuffix,
1506
- /* const*/ forMeta || FieldLayout->isFixedSize ());
1517
+ /* const*/ forMeta || FieldLayout->isFixedSize (),
1518
+ llvm::GlobalVariable::InternalLinkage);
1507
1519
}
1508
1520
1509
1521
private:
@@ -1723,7 +1735,8 @@ namespace {
1723
1735
1724
1736
// / Emit the method list and add the pointer to the `builder`.
1725
1737
void emitAndAddMethodList (ConstantInitBuilder::StructBuilder &builder,
1726
- MethodListKind kind) {
1738
+ MethodListKind kind,
1739
+ llvm::GlobalValue::LinkageTypes linkage) {
1727
1740
ArrayRef<MethodDescriptor> methods;
1728
1741
StringRef namePrefix;
1729
1742
switch (kind) {
@@ -1748,7 +1761,8 @@ namespace {
1748
1761
namePrefix = " _PROTOCOL_INSTANCE_METHODS_OPT_" ;
1749
1762
break ;
1750
1763
}
1751
- llvm::Constant *methodListPtr = buildMethodList (methods, namePrefix);
1764
+ llvm::Constant *methodListPtr =
1765
+ buildMethodList (methods, namePrefix, linkage);
1752
1766
builder.add (methodListPtr);
1753
1767
}
1754
1768
@@ -1768,12 +1782,9 @@ namespace {
1768
1782
return null ();
1769
1783
}
1770
1784
1771
- auto *gv_as_const = buildGlobalVariable (array, " _PROTOCOL_METHOD_TYPES_" ,
1772
- /* const*/ true ,
1773
- /* likage*/ llvm::GlobalVariable::WeakAnyLinkage);
1774
- llvm::GlobalValue *gv = (llvm::GlobalValue *)gv_as_const;
1775
- gv->setVisibility (llvm::GlobalValue::HiddenVisibility);
1776
- return gv;
1785
+ return buildGlobalVariable (array, " _PROTOCOL_METHOD_TYPES_" ,
1786
+ /* const*/ true ,
1787
+ llvm::GlobalVariable::WeakAnyLinkage);
1777
1788
}
1778
1789
1779
1790
void buildExtMethodTypes (ConstantArrayBuilder &array,
@@ -1796,13 +1807,14 @@ namespace {
1796
1807
// /
1797
1808
// / This method does not return a value of a predictable type.
1798
1809
llvm::Constant *buildMethodList (ArrayRef<MethodDescriptor> methods,
1799
- StringRef name) {
1800
- return buildOptionalList (methods, 3 * IGM.getPointerSize (), name,
1801
- /* isConst*/ false ,
1802
- [&](ConstantArrayBuilder &descriptors,
1803
- MethodDescriptor descriptor) {
1804
- buildMethod (descriptors, descriptor);
1805
- });
1810
+ StringRef name,
1811
+ llvm::GlobalValue::LinkageTypes linkage) {
1812
+ return buildOptionalList (
1813
+ methods, 3 * IGM.getPointerSize (), name,
1814
+ /* isConst*/ false , linkage,
1815
+ [&](ConstantArrayBuilder &descriptors, MethodDescriptor descriptor) {
1816
+ buildMethod (descriptors, descriptor);
1817
+ });
1806
1818
}
1807
1819
1808
1820
/* ** Protocols *********************************************************/
@@ -1819,16 +1831,15 @@ namespace {
1819
1831
// / };
1820
1832
// /
1821
1833
// / This method does not return a value of a predictable type.
1822
- llvm::Constant *buildProtocolList () {
1823
- return buildOptionalList (Protocols, Size (0 ),
1824
- chooseNamePrefix (" _PROTOCOLS_" ,
1825
- " _CATEGORY_PROTOCOLS_" ,
1826
- " _PROTOCOL_PROTOCOLS_" ),
1827
- /* isConst*/ true ,
1828
- [&](ConstantArrayBuilder &descriptors,
1829
- ProtocolDecl *protocol) {
1830
- buildProtocol (descriptors, protocol);
1831
- });
1834
+ llvm::Constant *buildProtocolList (llvm::GlobalValue::LinkageTypes linkage) {
1835
+ return buildOptionalList (
1836
+ Protocols, Size (0 ),
1837
+ chooseNamePrefix (" _PROTOCOLS_" , " _CATEGORY_PROTOCOLS_" ,
1838
+ " _PROTOCOL_PROTOCOLS_" ),
1839
+ /* isConst*/ true , linkage,
1840
+ [&](ConstantArrayBuilder &descriptors, ProtocolDecl *protocol) {
1841
+ buildProtocol (descriptors, protocol);
1842
+ });
1832
1843
}
1833
1844
1834
1845
void buildProtocol (ConstantArrayBuilder &array, ProtocolDecl *protocol) {
@@ -1949,12 +1960,12 @@ namespace {
1949
1960
// / This method does not return a value of a predictable type.
1950
1961
llvm::Constant *buildIvarList () {
1951
1962
Size eltSize = 3 * IGM.getPointerSize () + Size (8 );
1952
- return buildOptionalList (Ivars, eltSize, " _IVARS_ " ,
1953
- /* constant */ true ,
1954
- [&](ConstantArrayBuilder &descriptors ,
1955
- Field field) {
1956
- buildIvar (descriptors, field);
1957
- });
1963
+ return buildOptionalList (
1964
+ Ivars, eltSize, " _IVARS_ " ,
1965
+ /* constant */ true , llvm::GlobalVariable::InternalLinkage ,
1966
+ [&](ConstantArrayBuilder &descriptors, Field field) {
1967
+ buildIvar (descriptors, field);
1968
+ });
1958
1969
}
1959
1970
1960
1971
/* ** Properties ********************************************************/
@@ -2062,12 +2073,14 @@ namespace {
2062
2073
// / };
2063
2074
// /
2064
2075
// / This method does not return a value of a predictable type.
2065
- llvm::Constant *buildPropertyList (ForMetaClass_t classOrMeta) {
2076
+ llvm::Constant *buildPropertyList (ForMetaClass_t classOrMeta,
2077
+ llvm::GlobalValue::LinkageTypes linkage) {
2066
2078
if (classOrMeta == ForClass) {
2067
2079
return buildPropertyList (InstanceProperties,
2068
2080
chooseNamePrefix (" _PROPERTIES_" ,
2069
2081
" _CATEGORY_PROPERTIES_" ,
2070
- " _PROTOCOL_PROPERTIES_" ));
2082
+ " _PROTOCOL_PROPERTIES_" ),
2083
+ linkage);
2071
2084
}
2072
2085
2073
2086
// Older OSs' libobjcs can't handle class property data.
@@ -2079,18 +2092,20 @@ namespace {
2079
2092
return buildPropertyList (ClassProperties,
2080
2093
chooseNamePrefix (" _CLASS_PROPERTIES_" ,
2081
2094
" _CATEGORY_CLASS_PROPERTIES_" ,
2082
- " _PROTOCOL_CLASS_PROPERTIES_" ));
2095
+ " _PROTOCOL_CLASS_PROPERTIES_" ),
2096
+ linkage);
2083
2097
}
2084
2098
2085
- llvm::Constant *buildPropertyList (ArrayRef<VarDecl*> properties,
2086
- StringRef namePrefix) {
2099
+ llvm::Constant *buildPropertyList (ArrayRef<VarDecl *> properties,
2100
+ StringRef namePrefix,
2101
+ llvm::GlobalValue::LinkageTypes linkage) {
2087
2102
Size eltSize = 2 * IGM.getPointerSize ();
2088
- return buildOptionalList (properties, eltSize, namePrefix,
2089
- /* constant */ true ,
2090
- [&](ConstantArrayBuilder &descriptors ,
2091
- VarDecl *property) {
2092
- buildProperty (descriptors, property);
2093
- });
2103
+ return buildOptionalList (
2104
+ properties, eltSize, namePrefix ,
2105
+ /* constant */ true , linkage ,
2106
+ [&](ConstantArrayBuilder &descriptors, VarDecl *property) {
2107
+ buildProperty (descriptors, property);
2108
+ });
2094
2109
}
2095
2110
2096
2111
/* ** General ***********************************************************/
@@ -2102,10 +2117,9 @@ namespace {
2102
2117
// / \param optionalEltSize - if non-zero, a size which needs
2103
2118
// / to be placed in the list header
2104
2119
template <class C , class Fn >
2105
- llvm::Constant *buildOptionalList (const C &objects,
2106
- Size optionalEltSize,
2107
- StringRef nameBase,
2108
- bool isConst,
2120
+ llvm::Constant *buildOptionalList (const C &objects, Size optionalEltSize,
2121
+ StringRef nameBase, bool isConst,
2122
+ llvm::GlobalValue::LinkageTypes linkage,
2109
2123
Fn &&buildElement) {
2110
2124
if (objects.empty ())
2111
2125
return null ();
@@ -2144,9 +2158,9 @@ namespace {
2144
2158
2145
2159
fields.fillPlaceholderWithInt (countPosition, countType, count);
2146
2160
2147
- return buildGlobalVariable (fields, nameBase, isConst);
2161
+ return buildGlobalVariable (fields, nameBase, isConst, linkage );
2148
2162
}
2149
-
2163
+
2150
2164
// / Get the name of the class or protocol to mangle into the ObjC symbol
2151
2165
// / name.
2152
2166
StringRef getEntityName (llvm::SmallVectorImpl<char > &buffer) const {
@@ -2173,9 +2187,9 @@ namespace {
2173
2187
// / Build a private global variable as a structure containing the
2174
2188
// / given fields.
2175
2189
template <class B >
2176
- llvm::Constant *buildGlobalVariable (B &fields, StringRef nameBase, bool isConst,
2177
- llvm::GlobalValue::LinkageTypes linkage =
2178
- llvm::GlobalVariable::InternalLinkage ) {
2190
+ llvm::Constant *
2191
+ buildGlobalVariable (B &fields, StringRef nameBase, bool isConst,
2192
+ llvm::GlobalValue::LinkageTypes linkage ) {
2179
2193
llvm::SmallString<64 > nameBuffer;
2180
2194
auto var =
2181
2195
fields.finishAndCreateGlobal (Twine (nameBase)
@@ -2186,6 +2200,9 @@ namespace {
2186
2200
IGM.getPointerAlignment (),
2187
2201
/* constant*/ true ,
2188
2202
linkage);
2203
+ if (linkage == llvm::GlobalVariable::WeakAnyLinkage) {
2204
+ var->setVisibility (llvm::GlobalValue::HiddenVisibility);
2205
+ }
2189
2206
2190
2207
switch (IGM.TargetInfo .OutputObjectFormat ) {
2191
2208
case llvm::Triple::MachO:
0 commit comments