Skip to content

Commit b6d87cf

Browse files
committedJul 24, 2018
Borrow visibility from __fundamental_type_info for generated fundamental type infos
This is necessary so the clang gives hidden visibility to fundamental types when -fvisibility=hidden is passed. Fixes https://bugs.llvm.org/show_bug.cgi?id=35066 Differential Revision: https://reviews.llvm.org/D49109 llvm-svn: 337788
1 parent d6ff43c commit b6d87cf

File tree

2 files changed

+137
-61
lines changed

2 files changed

+137
-61
lines changed
 

‎clang/lib/CodeGen/ItaniumCXXABI.cpp

+67-60
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/AST/StmtCXX.h"
3232
#include "llvm/IR/CallSite.h"
3333
#include "llvm/IR/DataLayout.h"
34+
#include "llvm/IR/GlobalValue.h"
3435
#include "llvm/IR/Instructions.h"
3536
#include "llvm/IR/Intrinsics.h"
3637
#include "llvm/IR/Value.h"
@@ -181,8 +182,7 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI {
181182
emitTerminateForUnexpectedException(CodeGenFunction &CGF,
182183
llvm::Value *Exn) override;
183184

184-
void EmitFundamentalRTTIDescriptor(QualType Type, bool DLLExport);
185-
void EmitFundamentalRTTIDescriptors(bool DLLExport);
185+
void EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD);
186186
llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
187187
CatchTypeInfo
188188
getAddrOfCXXCatchHandlerType(QualType Ty,
@@ -1613,7 +1613,7 @@ void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
16131613
isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
16141614
cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
16151615
DC->getParent()->isTranslationUnit())
1616-
EmitFundamentalRTTIDescriptors(RD->hasAttr<DLLExportAttr>());
1616+
EmitFundamentalRTTIDescriptors(RD);
16171617

16181618
if (!VTable->isDeclarationForLinker())
16191619
CGM.EmitVTableTypeMetadata(VTable, VTLayout);
@@ -2698,12 +2698,16 @@ class ItaniumRTTIBuilder {
26982698
BCTI_Public = 0x2
26992699
};
27002700

2701+
/// BuildTypeInfo - Build the RTTI type info struct for the given type, or
2702+
/// link to an existing RTTI descriptor if one already exists.
2703+
llvm::Constant *BuildTypeInfo(QualType Ty);
2704+
27012705
/// BuildTypeInfo - Build the RTTI type info struct for the given type.
2702-
///
2703-
/// \param Force - true to force the creation of this RTTI value
2704-
/// \param DLLExport - true to mark the RTTI value as DLLExport
2705-
llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false,
2706-
bool DLLExport = false);
2706+
llvm::Constant *BuildTypeInfo(
2707+
QualType Ty,
2708+
llvm::GlobalVariable::LinkageTypes Linkage,
2709+
llvm::GlobalValue::VisibilityTypes Visibility,
2710+
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass);
27072711
};
27082712
}
27092713

@@ -3172,8 +3176,7 @@ static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
31723176
llvm_unreachable("Invalid linkage!");
31733177
}
31743178

3175-
llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force,
3176-
bool DLLExport) {
3179+
llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
31773180
// We want to operate on the canonical type.
31783181
Ty = Ty.getCanonicalType();
31793182

@@ -3191,17 +3194,41 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force,
31913194
}
31923195

31933196
// Check if there is already an external RTTI descriptor for this type.
3194-
bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
3195-
if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
3197+
if (IsStandardLibraryRTTIDescriptor(Ty) ||
3198+
ShouldUseExternalRTTIDescriptor(CGM, Ty))
31963199
return GetAddrOfExternalRTTIDescriptor(Ty);
31973200

31983201
// Emit the standard library with external linkage.
3199-
llvm::GlobalVariable::LinkageTypes Linkage;
3200-
if (IsStdLib)
3201-
Linkage = llvm::GlobalValue::ExternalLinkage;
3202+
llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
3203+
3204+
// Give the type_info object and name the formal visibility of the
3205+
// type itself.
3206+
llvm::GlobalValue::VisibilityTypes llvmVisibility;
3207+
if (llvm::GlobalValue::isLocalLinkage(Linkage))
3208+
// If the linkage is local, only default visibility makes sense.
3209+
llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3210+
else if (CXXABI.classifyRTTIUniqueness(Ty, Linkage) ==
3211+
ItaniumCXXABI::RUK_NonUniqueHidden)
3212+
llvmVisibility = llvm::GlobalValue::HiddenVisibility;
32023213
else
3203-
Linkage = getTypeInfoLinkage(CGM, Ty);
3214+
llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3215+
3216+
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3217+
llvm::GlobalValue::DefaultStorageClass;
3218+
if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3219+
auto RD = Ty->getAsCXXRecordDecl();
3220+
if (RD && RD->hasAttr<DLLExportAttr>())
3221+
DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
3222+
}
32043223

3224+
return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
3225+
}
3226+
3227+
llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
3228+
QualType Ty,
3229+
llvm::GlobalVariable::LinkageTypes Linkage,
3230+
llvm::GlobalValue::VisibilityTypes Visibility,
3231+
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass) {
32053232
// Add the vtable pointer.
32063233
BuildVTablePointer(cast<Type>(Ty));
32073234

@@ -3315,7 +3342,11 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force,
33153342

33163343
llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
33173344

3345+
SmallString<256> Name;
3346+
llvm::raw_svector_ostream Out(Name);
3347+
CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
33183348
llvm::Module &M = CGM.getModule();
3349+
llvm::GlobalVariable *OldGV = M.getNamedGlobal(Name);
33193350
llvm::GlobalVariable *GV =
33203351
new llvm::GlobalVariable(M, Init->getType(),
33213352
/*Constant=*/true, Linkage, Init, Name);
@@ -3347,40 +3378,14 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force,
33473378
// All of this is to say that it's important that both the type_info
33483379
// object and the type_info name be uniqued when weakly emitted.
33493380

3350-
// Give the type_info object and name the formal visibility of the
3351-
// type itself.
3352-
llvm::GlobalValue::VisibilityTypes llvmVisibility;
3353-
if (llvm::GlobalValue::isLocalLinkage(Linkage))
3354-
// If the linkage is local, only default visibility makes sense.
3355-
llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3356-
else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden)
3357-
llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3358-
else
3359-
llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3360-
3361-
TypeName->setVisibility(llvmVisibility);
3381+
TypeName->setVisibility(Visibility);
33623382
CGM.setDSOLocal(TypeName);
33633383

3364-
GV->setVisibility(llvmVisibility);
3384+
GV->setVisibility(Visibility);
33653385
CGM.setDSOLocal(GV);
33663386

3367-
if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3368-
auto RD = Ty->getAsCXXRecordDecl();
3369-
if (DLLExport || (RD && RD->hasAttr<DLLExportAttr>())) {
3370-
TypeName->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
3371-
GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
3372-
} else if (RD && RD->hasAttr<DLLImportAttr>() &&
3373-
ShouldUseExternalRTTIDescriptor(CGM, Ty)) {
3374-
TypeName->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3375-
GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3376-
3377-
// Because the typename and the typeinfo are DLL import, convert them to
3378-
// declarations rather than definitions. The initializers still need to
3379-
// be constructed to calculate the type for the declarations.
3380-
TypeName->setInitializer(nullptr);
3381-
GV->setInitializer(nullptr);
3382-
}
3383-
}
3387+
TypeName->setDLLStorageClass(DLLStorageClass);
3388+
GV->setDLLStorageClass(DLLStorageClass);
33843389

33853390
return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
33863391
}
@@ -3655,18 +3660,7 @@ llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
36553660
return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
36563661
}
36573662

3658-
void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type,
3659-
bool DLLExport) {
3660-
QualType PointerType = getContext().getPointerType(Type);
3661-
QualType PointerTypeConst = getContext().getPointerType(Type.withConst());
3662-
ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, /*Force=*/true, DLLExport);
3663-
ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, /*Force=*/true,
3664-
DLLExport);
3665-
ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, /*Force=*/true,
3666-
DLLExport);
3667-
}
3668-
3669-
void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(bool DLLExport) {
3663+
void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
36703664
// Types added here must also be added to TypeInfoIsInStandardLibrary.
36713665
QualType FundamentalTypes[] = {
36723666
getContext().VoidTy, getContext().NullPtrTy,
@@ -3683,8 +3677,21 @@ void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(bool DLLExport) {
36833677
getContext().Char8Ty, getContext().Char16Ty,
36843678
getContext().Char32Ty
36853679
};
3686-
for (const QualType &FundamentalType : FundamentalTypes)
3687-
EmitFundamentalRTTIDescriptor(FundamentalType, DLLExport);
3680+
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3681+
RD->hasAttr<DLLExportAttr>()
3682+
? llvm::GlobalValue::DLLExportStorageClass
3683+
: llvm::GlobalValue::DefaultStorageClass;
3684+
llvm::GlobalValue::VisibilityTypes Visibility =
3685+
CodeGenModule::GetLLVMVisibility(RD->getVisibility());
3686+
for (const QualType &FundamentalType : FundamentalTypes) {
3687+
QualType PointerType = getContext().getPointerType(FundamentalType);
3688+
QualType PointerTypeConst = getContext().getPointerType(
3689+
FundamentalType.withConst());
3690+
for (QualType Type : {FundamentalType, PointerType, PointerTypeConst})
3691+
ItaniumRTTIBuilder(*this).BuildTypeInfo(
3692+
Type, llvm::GlobalValue::ExternalLinkage,
3693+
Visibility, DLLStorageClass);
3694+
}
36883695
}
36893696

36903697
/// What sort of uniqueness rules should we use for the RTTI for the

‎clang/test/CodeGenCXX/rtti-fundamental.cpp

+70-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -fvisibility hidden -o - | FileCheck %s -check-prefix=CHECK-HIDDEN
23

34
#include <typeinfo>
45

@@ -16,116 +17,184 @@ namespace __cxxabiv1 {
1617

1718
// void
1819
// CHECK: @_ZTIv = constant
20+
// CHECK-HIDDEN: @_ZTIv = hidden constant
1921
// CHECK: @_ZTIPv = constant
22+
// CHECK-HIDDEN: @_ZTIPv = hidden constant
2023
// CHECK: @_ZTIPKv = constant
24+
// CHECK-HIDDEN: @_ZTIPKv = hidden constant
2125

2226
// std::nullptr_t
2327
// CHECK: @_ZTIDn = constant
28+
// CHECK-HIDDEN: @_ZTIDn = hidden constant
2429
// CHECK: @_ZTIPDn = constant
30+
// CHECK-HIDDEN: @_ZTIPDn = hidden constant
2531
// CHECK: @_ZTIPKDn = constant
32+
// CHECK-HIDDEN: @_ZTIPKDn = hidden constant
2633

2734
// bool
2835
// CHECK: @_ZTIb = constant
36+
// CHECK-HIDDEN: @_ZTIb = hidden constant
2937
// CHECK: @_ZTIPb = constant
38+
// CHECK-HIDDEN: @_ZTIPb = hidden constant
3039
// CHECK: @_ZTIPKb = constant
40+
// CHECK-HIDDEN: @_ZTIPKb = hidden constant
3141

3242
// wchar_t
3343
// CHECK: @_ZTIw = constant
44+
// CHECK-HIDDEN: @_ZTIw = hidden constant
3445
// CHECK: @_ZTIPw = constant
46+
// CHECK-HIDDEN: @_ZTIPw = hidden constant
3547
// CHECK: @_ZTIPKw = constant
48+
// CHECK-HIDDEN: @_ZTIPKw = hidden constant
3649

3750
// char
3851
// CHECK: @_ZTIc = constant
52+
// CHECK-HIDDEN: @_ZTIc = hidden constant
3953
// CHECK: @_ZTIPc = constant
54+
// CHECK-HIDDEN: @_ZTIPc = hidden constant
4055
// CHECK: @_ZTIPKc = constant
56+
// CHECK-HIDDEN: @_ZTIPKc = hidden constant
4157

4258
// unsigned char
4359
// CHECK: @_ZTIh = constant
60+
// CHECK-HIDDEN: @_ZTIh = hidden constant
4461
// CHECK: @_ZTIPh = constant
62+
// CHECK-HIDDEN: @_ZTIPh = hidden constant
4563
// CHECK: @_ZTIPKh = constant
64+
// CHECK-HIDDEN: @_ZTIPKh = hidden constant
4665

4766
// signed char
4867
// CHECK: @_ZTIa = constant
68+
// CHECK-HIDDEN: @_ZTIa = hidden constant
4969
// CHECK: @_ZTIPa = constant
70+
// CHECK-HIDDEN: @_ZTIPa = hidden constant
5071
// CHECK: @_ZTIPKa = constant
72+
// CHECK-HIDDEN: @_ZTIPKa = hidden constant
5173

5274
// short
5375
// CHECK: @_ZTIs = constant
76+
// CHECK-HIDDEN: @_ZTIs = hidden constant
5477
// CHECK: @_ZTIPs = constant
78+
// CHECK-HIDDEN: @_ZTIPs = hidden constant
5579
// CHECK: @_ZTIPKs = constant
80+
// CHECK-HIDDEN: @_ZTIPKs = hidden constant
5681

5782
// unsigned short
5883
// CHECK: @_ZTIt = constant
84+
// CHECK-HIDDEN: @_ZTIt = hidden constant
5985
// CHECK: @_ZTIPt = constant
86+
// CHECK-HIDDEN: @_ZTIPt = hidden constant
6087
// CHECK: @_ZTIPKt = constant
88+
// CHECK-HIDDEN: @_ZTIPKt = hidden constant
6189

6290
// int
6391
// CHECK: @_ZTIi = constant
92+
// CHECK-HIDDEN: @_ZTIi = hidden constant
6493
// CHECK: @_ZTIPi = constant
94+
// CHECK-HIDDEN: @_ZTIPi = hidden constant
6595
// CHECK: @_ZTIPKi = constant
96+
// CHECK-HIDDEN: @_ZTIPKi = hidden constant
6697

6798
// unsigned int
6899
// CHECK: @_ZTIj = constant
100+
// CHECK-HIDDEN: @_ZTIj = hidden constant
69101
// CHECK: @_ZTIPj = constant
102+
// CHECK-HIDDEN: @_ZTIPj = hidden constant
70103
// CHECK: @_ZTIPKj = constant
104+
// CHECK-HIDDEN: @_ZTIPKj = hidden constant
71105

72106
// long
73107
// CHECK: @_ZTIl = constant
108+
// CHECK-HIDDEN: @_ZTIl = hidden constant
74109
// CHECK: @_ZTIPl = constant
110+
// CHECK-HIDDEN: @_ZTIPl = hidden constant
75111
// CHECK: @_ZTIPKl = constant
112+
// CHECK-HIDDEN: @_ZTIPKl = hidden constant
76113

77114
// unsigned long
78115
// CHECK: @_ZTIm = constant
116+
// CHECK-HIDDEN: @_ZTIm = hidden constant
79117
// CHECK: @_ZTIPm = constant
118+
// CHECK-HIDDEN: @_ZTIPm = hidden constant
80119
// CHECK: @_ZTIPKm = constant
120+
// CHECK-HIDDEN: @_ZTIPKm = hidden constant
81121

82122
// long long
83123
// CHECK: @_ZTIx = constant
124+
// CHECK-HIDDEN: @_ZTIx = hidden constant
84125
// CHECK: @_ZTIPx = constant
126+
// CHECK-HIDDEN: @_ZTIPx = hidden constant
85127
// CHECK: @_ZTIPKx = constant
128+
// CHECK-HIDDEN: @_ZTIPKx = hidden constant
86129

87130
// unsigned long long
88131
// CHECK: @_ZTIy = constant
132+
// CHECK-HIDDEN: @_ZTIy = hidden constant
89133
// CHECK: @_ZTIPy = constant
134+
// CHECK-HIDDEN: @_ZTIPy = hidden constant
90135
// CHECK: @_ZTIPKy = constant
136+
// CHECK-HIDDEN: @_ZTIPKy = hidden constant
91137

92138
// __int128
93139
// CHECK: @_ZTIn = constant
140+
// CHECK-HIDDEN: @_ZTIn = hidden constant
94141
// CHECK: @_ZTIPn = constant
142+
// CHECK-HIDDEN: @_ZTIPn = hidden constant
95143
// CHECK: @_ZTIPKn = constant
144+
// CHECK-HIDDEN: @_ZTIPKn = hidden constant
96145

97146
// unsigned __int128
98147
// CHECK: @_ZTIo = constant
148+
// CHECK-HIDDEN: @_ZTIo = hidden constant
99149
// CHECK: @_ZTIPo = constant
150+
// CHECK-HIDDEN: @_ZTIPo = hidden constant
100151
// CHECK: @_ZTIPKo = constant
152+
// CHECK-HIDDEN: @_ZTIPKo = hidden constant
101153

102154
// half
103155
// CHECK: @_ZTIDh = constant
156+
// CHECK-HIDDEN: @_ZTIDh = hidden constant
104157
// CHECK: @_ZTIPDh = constant
158+
// CHECK-HIDDEN: @_ZTIPDh = hidden constant
105159
// CHECK: @_ZTIPKDh = constant
160+
// CHECK-HIDDEN: @_ZTIPKDh = hidden constant
106161

107162
// float
108163
// CHECK: @_ZTIf = constant
164+
// CHECK-HIDDEN: @_ZTIf = hidden constant
109165
// CHECK: @_ZTIPf = constant
166+
// CHECK-HIDDEN: @_ZTIPf = hidden constant
110167
// CHECK: @_ZTIPKf = constant
168+
// CHECK-HIDDEN: @_ZTIPKf = hidden constant
111169

112170
// double
113171
// CHECK: @_ZTId = constant
172+
// CHECK-HIDDEN: @_ZTId = hidden constant
114173
// CHECK: @_ZTIPd = constant
174+
// CHECK-HIDDEN: @_ZTIPd = hidden constant
115175
// CHECK: @_ZTIPKd = constant
176+
// CHECK-HIDDEN: @_ZTIPKd = hidden constant
116177

117178
// long double
118179
// CHECK: @_ZTIe = constant
180+
// CHECK-HIDDEN: @_ZTIe = hidden constant
119181
// CHECK: @_ZTIPe = constant
182+
// CHECK-HIDDEN: @_ZTIPe = hidden constant
120183
// CHECK: @_ZTIPKe = constant
184+
// CHECK-HIDDEN: @_ZTIPKe = hidden constant
121185

122186
// char16_t
123187
// CHECK: @_ZTIDs = constant
188+
// CHECK-HIDDEN: @_ZTIDs = hidden constant
124189
// CHECK: @_ZTIPDs = constant
190+
// CHECK-HIDDEN: @_ZTIPDs = hidden constant
125191
// CHECK: @_ZTIPKDs = constant
192+
// CHECK-HIDDEN: @_ZTIPKDs = hidden constant
126193

127194
// char32_t
128195
// CHECK: @_ZTIDi = constant
196+
// CHECK-HIDDEN: @_ZTIDi = hidden constant
129197
// CHECK: @_ZTIPDi = constant
198+
// CHECK-HIDDEN: @_ZTIPDi = hidden constant
130199
// CHECK: @_ZTIPKDi = constant
131-
200+
// CHECK-HIDDEN: @_ZTIPKDi = hidden constant

0 commit comments

Comments
 (0)
Please sign in to comment.