1 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code dealing with C++ code generation of VTTs (vtable tables). 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeGenModule.h" 14 #include "CGCXXABI.h" 15 #include "clang/AST/RecordLayout.h" 16 #include "clang/AST/VTTBuilder.h" 17 using namespace clang; 18 using namespace CodeGen; 19 20 static llvm::GlobalVariable * 21 GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, 22 const CXXRecordDecl *MostDerivedClass, 23 const VTTVTable &VTable, 24 llvm::GlobalVariable::LinkageTypes Linkage, 25 VTableLayout::AddressPointsMapTy &AddressPoints) { 26 if (VTable.getBase() == MostDerivedClass) { 27 assert(VTable.getBaseOffset().isZero() && 28 "Most derived class vtable must have a zero offset!"); 29 // This is a regular vtable. 30 return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits()); 31 } 32 33 return CGVT.GenerateConstructionVTable(MostDerivedClass, 34 VTable.getBaseSubobject(), 35 VTable.isVirtual(), 36 Linkage, 37 AddressPoints); 38 } 39 40 void 41 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT, 42 llvm::GlobalVariable::LinkageTypes Linkage, 43 const CXXRecordDecl *RD) { 44 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true); 45 llvm::ArrayType *ArrayType = llvm::ArrayType::get( 46 CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size()); 47 48 SmallVector<llvm::GlobalVariable *, 8> VTables; 49 SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints; 50 for (const VTTVTable *i = Builder.getVTTVTables().begin(), 51 *e = Builder.getVTTVTables().end(); i != e; ++i) { 52 VTableAddressPoints.push_back(VTableAddressPointsMapTy()); 53 VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage, 54 VTableAddressPoints.back())); 55 } 56 57 SmallVector<llvm::Constant *, 8> VTTComponents; 58 for (const VTTComponent *i = Builder.getVTTComponents().begin(), 59 *e = Builder.getVTTComponents().end(); i != e; ++i) { 60 const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex]; 61 llvm::GlobalVariable *VTable = VTables[i->VTableIndex]; 62 VTableLayout::AddressPointLocation AddressPoint; 63 if (VTTVT.getBase() == RD) { 64 // Just get the address point for the regular vtable. 65 AddressPoint = 66 getItaniumVTableContext().getVTableLayout(RD).getAddressPoint( 67 i->VTableBase); 68 } else { 69 AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase); 70 assert(AddressPoint.AddressPointIndex != 0 && 71 "Did not find ctor vtable address point!"); 72 } 73 74 llvm::Value *Idxs[] = { 75 llvm::ConstantInt::get(CGM.Int32Ty, 0), 76 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex), 77 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex), 78 }; 79 80 // Add inrange attribute to indicate that only the VTableIndex can be 81 // accessed. 82 unsigned ComponentSize = 83 CGM.getDataLayout().getTypeAllocSize(getVTableComponentType()); 84 unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize( 85 cast<llvm::StructType>(VTable->getValueType()) 86 ->getElementType(AddressPoint.VTableIndex)); 87 unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex; 88 llvm::ConstantRange InRange( 89 llvm::APInt(32, (int)-Offset, true), 90 llvm::APInt(32, (int)(VTableSize - Offset), true)); 91 llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr( 92 VTable->getValueType(), VTable, Idxs, /*InBounds=*/true, InRange); 93 94 if (const auto &Schema = 95 CGM.getCodeGenOpts().PointerAuth.CXXVTTVTablePointers) 96 Init = CGM.getConstantSignedPointer(Init, Schema, nullptr, GlobalDecl(), 97 QualType()); 98 99 VTTComponents.push_back(Init); 100 } 101 102 llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents); 103 104 VTT->setInitializer(Init); 105 106 // Set the correct linkage. 107 VTT->setLinkage(Linkage); 108 109 if (CGM.supportsCOMDAT() && VTT->isWeakForLinker()) 110 VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName())); 111 112 // Set the visibility. This will already have been set on the VTT declaration. 113 // Set it again, now that we have a definition, as the implicit visibility can 114 // apply differently to definitions. 115 CGM.setGVProperties(VTT, RD); 116 } 117 118 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) { 119 assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT"); 120 121 SmallString<256> OutName; 122 llvm::raw_svector_ostream Out(OutName); 123 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) 124 .mangleCXXVTT(RD, Out); 125 StringRef Name = OutName.str(); 126 127 // This will also defer the definition of the VTT. 128 (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); 129 130 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 131 132 llvm::ArrayType *ArrayType = llvm::ArrayType::get( 133 CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size()); 134 llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.GlobalsInt8PtrTy); 135 136 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable( 137 Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align); 138 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 139 CGM.setGVProperties(GV, RD); 140 return GV; 141 } 142 143 uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD, 144 BaseSubobject Base) { 145 BaseSubobjectPairTy ClassSubobjectPair(RD, Base); 146 147 SubVTTIndicesMapTy::iterator I = SubVTTIndices.find(ClassSubobjectPair); 148 if (I != SubVTTIndices.end()) 149 return I->second; 150 151 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 152 153 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator 154 I = Builder.getSubVTTIndices().begin(), 155 E = Builder.getSubVTTIndices().end(); 156 I != E; ++I) { 157 // Insert all indices. 158 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first); 159 160 SubVTTIndices.insert(std::make_pair(ClassSubobjectPair, I->second)); 161 } 162 163 I = SubVTTIndices.find(ClassSubobjectPair); 164 assert(I != SubVTTIndices.end() && "Did not find index!"); 165 166 return I->second; 167 } 168 169 uint64_t 170 CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 171 BaseSubobject Base) { 172 SecondaryVirtualPointerIndicesMapTy::iterator I = 173 SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 174 175 if (I != SecondaryVirtualPointerIndices.end()) 176 return I->second; 177 178 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 179 180 // Insert all secondary vpointer indices. 181 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = 182 Builder.getSecondaryVirtualPointerIndices().begin(), 183 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) { 184 std::pair<const CXXRecordDecl *, BaseSubobject> Pair = 185 std::make_pair(RD, I->first); 186 187 SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second)); 188 } 189 190 I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 191 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!"); 192 193 return I->second; 194 } 195