| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "CGCXXABI.h" |
| 17 | #include "CodeGenFunction.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
| 22 | #include "clang/AST/Mangle.h" |
| 23 | #include "clang/AST/RecordLayout.h" |
| 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/Basic/CodeGenOptions.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 34 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 35 | return true; |
| 36 | |
| 37 | |
| 38 | |
| 39 | if (getCodeGenOpts().OptimizationLevel == 0) |
| 40 | return true; |
| 41 | |
| 42 | |
| 43 | |
| 44 | if (getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 45 | !D->getParent()->field_empty()) |
| 46 | return true; |
| 47 | |
| 48 | |
| 49 | |
| 50 | if (!D->hasTrivialBody()) |
| 51 | return true; |
| 52 | |
| 53 | const CXXRecordDecl *Class = D->getParent(); |
| 54 | |
| 55 | |
| 56 | |
| 57 | if (Class->mayInsertExtraPadding()) |
| 58 | return true; |
| 59 | |
| 60 | |
| 61 | if (Class->getNumVBases()) { |
| 62 | |
| 63 | |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | for (const auto *I : Class->fields()) |
| 71 | if (I->getType().isDestructedType()) |
| 72 | return true; |
| 73 | |
| 74 | |
| 75 | const CXXRecordDecl *UniqueBase = nullptr; |
| 76 | for (const auto &I : Class->bases()) { |
| 77 | |
| 78 | |
| 79 | if (I.isVirtual()) continue; |
| 80 | |
| 81 | |
| 82 | const auto *Base = |
| 83 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
| 84 | if (Base->hasTrivialDestructor()) continue; |
| 85 | |
| 86 | |
| 87 | |
| 88 | if (UniqueBase) return true; |
| 89 | UniqueBase = Base; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | if (!UniqueBase) |
| 97 | return true; |
| 98 | |
| 99 | |
| 100 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
| 101 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
| 102 | return true; |
| 103 | |
| 104 | |
| 105 | |
| 106 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
| 107 | if (BaseD->getType()->getAs<FunctionType>()->getCallConv() != |
| 108 | D->getType()->getAs<FunctionType>()->getCallConv()) |
| 109 | return true; |
| 110 | |
| 111 | GlobalDecl AliasDecl(D, Dtor_Base); |
| 112 | GlobalDecl TargetDecl(BaseD, Dtor_Base); |
| 113 | |
| 114 | |
| 115 | |
| 116 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
| 117 | |
| 118 | |
| 119 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
| 120 | return true; |
| 121 | |
| 122 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
| 123 | getFunctionLinkage(TargetDecl); |
| 124 | |
| 125 | |
| 126 | StringRef MangledName = getMangledName(AliasDecl); |
| 127 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 128 | if (Entry && !Entry->isDeclaration()) |
| 129 | return false; |
| 130 | if (Replacements.count(MangledName)) |
| 131 | return false; |
| 132 | |
| 133 | |
| 134 | llvm::Type *AliasValueType = getTypes().GetFunctionType(AliasDecl); |
| 135 | llvm::PointerType *AliasType = AliasValueType->getPointerTo(); |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 141 | llvm::Constant *Aliasee = Ref; |
| 142 | if (Ref->getType() != AliasType) |
| 143 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 144 | |
| 145 | |
| 146 | |
| 147 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
| 148 | !(TargetLinkage == llvm::GlobalValue::AvailableExternallyLinkage && |
| 149 | TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | addReplacement(MangledName, Aliasee); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | if (llvm::GlobalValue::isWeakForLinker(Linkage) && |
| 165 | getTriple().isOSBinFormatCOFF()) { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | if (Ref->isDeclarationForLinker()) |
| 173 | return true; |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
| 180 | return true; |
| 181 | |
| 182 | |
| 183 | auto *Alias = llvm::GlobalAlias::create(AliasValueType, 0, Linkage, "", |
| 184 | Aliasee, &getModule()); |
| 185 | |
| 186 | |
| 187 | Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 188 | |
| 189 | |
| 190 | if (Entry) { |
| 191 | (0) . __assert_fail ("Entry->getType() == AliasType && \"declaration exists with different type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 192, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Entry->getType() == AliasType && |
| 192 | (0) . __assert_fail ("Entry->getType() == AliasType && \"declaration exists with different type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 192, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "declaration exists with different type"); |
| 193 | Alias->takeName(Entry); |
| 194 | Entry->replaceAllUsesWith(Alias); |
| 195 | Entry->eraseFromParent(); |
| 196 | } else { |
| 197 | Alias->setName(MangledName); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | SetCommonAttributes(AliasDecl, Alias); |
| 202 | |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | llvm::Function *CodeGenModule::codegenCXXStructor(GlobalDecl GD) { |
| 207 | const CGFunctionInfo &FnInfo = getTypes().arrangeCXXStructorDeclaration(GD); |
| 208 | auto *Fn = cast<llvm::Function>( |
| 209 | getAddrOfCXXStructor(GD, &FnInfo, , |
| 210 | , ForDefinition)); |
| 211 | |
| 212 | setFunctionLinkage(GD, Fn); |
| 213 | |
| 214 | CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo); |
| 215 | setNonAliasAttributes(GD, Fn); |
| 216 | SetLLVMFunctionAttributesForDefinition(cast<CXXMethodDecl>(GD.getDecl()), Fn); |
| 217 | return Fn; |
| 218 | } |
| 219 | |
| 220 | llvm::FunctionCallee CodeGenModule::getAddrAndTypeOfCXXStructor( |
| 221 | GlobalDecl GD, const CGFunctionInfo *FnInfo, llvm::FunctionType *FnType, |
| 222 | bool DontDefer, ForDefinition_t IsForDefinition) { |
| 223 | auto *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 224 | |
| 225 | if (isa<CXXDestructorDecl>(MD)) { |
| 226 | |
| 227 | |
| 228 | if (getTarget().getCXXABI().isMicrosoft() && |
| 229 | GD.getDtorType() == Dtor_Complete && |
| 230 | MD->getParent()->getNumVBases() == 0) |
| 231 | GD = GD.getWithDtorType(Dtor_Base); |
| 232 | } |
| 233 | |
| 234 | if (!FnType) { |
| 235 | if (!FnInfo) |
| 236 | FnInfo = &getTypes().arrangeCXXStructorDeclaration(GD); |
| 237 | FnType = getTypes().GetFunctionType(*FnInfo); |
| 238 | } |
| 239 | |
| 240 | llvm::Constant *Ptr = GetOrCreateLLVMFunction( |
| 241 | getMangledName(GD), FnType, GD, , DontDefer, |
| 242 | , llvm::AttributeList(), IsForDefinition); |
| 243 | return {FnType, Ptr}; |
| 244 | } |
| 245 | |
| 246 | static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 247 | GlobalDecl GD, |
| 248 | llvm::Type *Ty, |
| 249 | const CXXRecordDecl *RD) { |
| 250 | (0) . __assert_fail ("!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && \"No kext in Microsoft ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 251, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 251 | (0) . __assert_fail ("!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && \"No kext in Microsoft ABI\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 251, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No kext in Microsoft ABI"); |
| 252 | CodeGenModule &CGM = CGF.CGM; |
| 253 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
| 254 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 255 | VTable = CGF.Builder.CreateBitCast(VTable, Ty); |
| 256 | (0) . __assert_fail ("VTable && \"BuildVirtualCall = kext vtbl pointer is null\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 256, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
| 257 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
| 258 | const VTableLayout &VTLayout = CGM.getItaniumVTableContext().getVTableLayout(RD); |
| 259 | VTableLayout::AddressPointLocation AddressPoint = |
| 260 | VTLayout.getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
| 261 | VTableIndex += VTLayout.getVTableOffset(AddressPoint.VTableIndex) + |
| 262 | AddressPoint.AddressPointIndex; |
| 263 | llvm::Value *VFuncPtr = |
| 264 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
| 265 | llvm::Value *VFunc = |
| 266 | CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes); |
| 267 | CGCallee Callee(GD, VFunc); |
| 268 | return Callee; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | CGCallee |
| 275 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 276 | NestedNameSpecifier *Qual, |
| 277 | llvm::Type *Ty) { |
| 278 | (0) . __assert_fail ("(Qual->getKind() == NestedNameSpecifier..TypeSpec) && \"BuildAppleKextVirtualCall - bad Qual kind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 279, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 279 | (0) . __assert_fail ("(Qual->getKind() == NestedNameSpecifier..TypeSpec) && \"BuildAppleKextVirtualCall - bad Qual kind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 279, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "BuildAppleKextVirtualCall - bad Qual kind"); |
| 280 | |
| 281 | const Type *QTy = Qual->getAsType(); |
| 282 | QualType T = QualType(QTy, 0); |
| 283 | const RecordType *RT = T->getAs<RecordType>(); |
| 284 | (0) . __assert_fail ("RT && \"BuildAppleKextVirtualCall - Qual type must be record\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 284, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
| 285 | const auto *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 286 | |
| 287 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 288 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
| 289 | |
| 290 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | |
| 295 | CGCallee |
| 296 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 297 | const CXXDestructorDecl *DD, |
| 298 | CXXDtorType Type, |
| 299 | const CXXRecordDecl *RD) { |
| 300 | isVirtual() && Type != Dtor_Base", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGCXX.cpp", 300, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DD->isVirtual() && Type != Dtor_Base); |
| 301 | |
| 302 | const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration( |
| 303 | GlobalDecl(DD, Dtor_Complete)); |
| 304 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
| 305 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
| 306 | } |
| 307 | |