10b57cec5SDimitry Andric //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This contains code dealing with C++ code generation of VTTs (vtable tables). 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "CodeGenModule.h" 140b57cec5SDimitry Andric #include "CGCXXABI.h" 150b57cec5SDimitry Andric #include "clang/AST/RecordLayout.h" 160b57cec5SDimitry Andric #include "clang/AST/VTTBuilder.h" 170b57cec5SDimitry Andric using namespace clang; 180b57cec5SDimitry Andric using namespace CodeGen; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric static llvm::GlobalVariable * 210b57cec5SDimitry Andric GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, 220b57cec5SDimitry Andric const CXXRecordDecl *MostDerivedClass, 230b57cec5SDimitry Andric const VTTVTable &VTable, 240b57cec5SDimitry Andric llvm::GlobalVariable::LinkageTypes Linkage, 250b57cec5SDimitry Andric VTableLayout::AddressPointsMapTy &AddressPoints) { 260b57cec5SDimitry Andric if (VTable.getBase() == MostDerivedClass) { 270b57cec5SDimitry Andric assert(VTable.getBaseOffset().isZero() && 280b57cec5SDimitry Andric "Most derived class vtable must have a zero offset!"); 290b57cec5SDimitry Andric // This is a regular vtable. 300b57cec5SDimitry Andric return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits()); 310b57cec5SDimitry Andric } 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric return CGVT.GenerateConstructionVTable(MostDerivedClass, 340b57cec5SDimitry Andric VTable.getBaseSubobject(), 350b57cec5SDimitry Andric VTable.isVirtual(), 360b57cec5SDimitry Andric Linkage, 370b57cec5SDimitry Andric AddressPoints); 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric void 410b57cec5SDimitry Andric CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT, 420b57cec5SDimitry Andric llvm::GlobalVariable::LinkageTypes Linkage, 430b57cec5SDimitry Andric const CXXRecordDecl *RD) { 440b57cec5SDimitry Andric VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true); 4506c3fb27SDimitry Andric llvm::ArrayType *ArrayType = llvm::ArrayType::get( 4606c3fb27SDimitry Andric CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size()); 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric SmallVector<llvm::GlobalVariable *, 8> VTables; 490b57cec5SDimitry Andric SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints; 500b57cec5SDimitry Andric for (const VTTVTable *i = Builder.getVTTVTables().begin(), 510b57cec5SDimitry Andric *e = Builder.getVTTVTables().end(); i != e; ++i) { 520b57cec5SDimitry Andric VTableAddressPoints.push_back(VTableAddressPointsMapTy()); 530b57cec5SDimitry Andric VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage, 540b57cec5SDimitry Andric VTableAddressPoints.back())); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric SmallVector<llvm::Constant *, 8> VTTComponents; 580b57cec5SDimitry Andric for (const VTTComponent *i = Builder.getVTTComponents().begin(), 590b57cec5SDimitry Andric *e = Builder.getVTTComponents().end(); i != e; ++i) { 600b57cec5SDimitry Andric const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex]; 610b57cec5SDimitry Andric llvm::GlobalVariable *VTable = VTables[i->VTableIndex]; 620b57cec5SDimitry Andric VTableLayout::AddressPointLocation AddressPoint; 630b57cec5SDimitry Andric if (VTTVT.getBase() == RD) { 640b57cec5SDimitry Andric // Just get the address point for the regular vtable. 650b57cec5SDimitry Andric AddressPoint = 660b57cec5SDimitry Andric getItaniumVTableContext().getVTableLayout(RD).getAddressPoint( 670b57cec5SDimitry Andric i->VTableBase); 680b57cec5SDimitry Andric } else { 690b57cec5SDimitry Andric AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase); 700b57cec5SDimitry Andric assert(AddressPoint.AddressPointIndex != 0 && 710b57cec5SDimitry Andric "Did not find ctor vtable address point!"); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric llvm::Value *Idxs[] = { 75e8d8bef9SDimitry Andric llvm::ConstantInt::get(CGM.Int32Ty, 0), 76e8d8bef9SDimitry Andric llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex), 77e8d8bef9SDimitry Andric llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex), 780b57cec5SDimitry Andric }; 790b57cec5SDimitry Andric 80*0fca6ea1SDimitry Andric // Add inrange attribute to indicate that only the VTableIndex can be 81*0fca6ea1SDimitry Andric // accessed. 82*0fca6ea1SDimitry Andric unsigned ComponentSize = 83*0fca6ea1SDimitry Andric CGM.getDataLayout().getTypeAllocSize(getVTableComponentType()); 84*0fca6ea1SDimitry Andric unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize( 85*0fca6ea1SDimitry Andric cast<llvm::StructType>(VTable->getValueType()) 86*0fca6ea1SDimitry Andric ->getElementType(AddressPoint.VTableIndex)); 87*0fca6ea1SDimitry Andric unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex; 88*0fca6ea1SDimitry Andric llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true), 89*0fca6ea1SDimitry Andric llvm::APInt(32, VTableSize - Offset, true)); 900b57cec5SDimitry Andric llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr( 91*0fca6ea1SDimitry Andric VTable->getValueType(), VTable, Idxs, /*InBounds=*/true, InRange); 92*0fca6ea1SDimitry Andric 93*0fca6ea1SDimitry Andric if (const auto &Schema = 94*0fca6ea1SDimitry Andric CGM.getCodeGenOpts().PointerAuth.CXXVTTVTablePointers) 95*0fca6ea1SDimitry Andric Init = CGM.getConstantSignedPointer(Init, Schema, nullptr, GlobalDecl(), 96*0fca6ea1SDimitry Andric QualType()); 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric VTTComponents.push_back(Init); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric VTT->setInitializer(Init); 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric // Set the correct linkage. 1060b57cec5SDimitry Andric VTT->setLinkage(Linkage); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric if (CGM.supportsCOMDAT() && VTT->isWeakForLinker()) 1090b57cec5SDimitry Andric VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName())); 1105f757f3fSDimitry Andric 1115f757f3fSDimitry Andric // Set the visibility. This will already have been set on the VTT declaration. 1125f757f3fSDimitry Andric // Set it again, now that we have a definition, as the implicit visibility can 1135f757f3fSDimitry Andric // apply differently to definitions. 1145f757f3fSDimitry Andric CGM.setGVProperties(VTT, RD); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) { 1180b57cec5SDimitry Andric assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT"); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric SmallString<256> OutName; 1210b57cec5SDimitry Andric llvm::raw_svector_ostream Out(OutName); 1220b57cec5SDimitry Andric cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) 1230b57cec5SDimitry Andric .mangleCXXVTT(RD, Out); 1240b57cec5SDimitry Andric StringRef Name = OutName.str(); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // This will also defer the definition of the VTT. 1270b57cec5SDimitry Andric (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 1300b57cec5SDimitry Andric 13106c3fb27SDimitry Andric llvm::ArrayType *ArrayType = llvm::ArrayType::get( 13206c3fb27SDimitry Andric CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size()); 13306c3fb27SDimitry Andric llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.GlobalsInt8PtrTy); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable( 1360b57cec5SDimitry Andric Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align); 1370b57cec5SDimitry Andric GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 13881ad6265SDimitry Andric CGM.setGVProperties(GV, RD); 1390b57cec5SDimitry Andric return GV; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD, 1430b57cec5SDimitry Andric BaseSubobject Base) { 1440b57cec5SDimitry Andric BaseSubobjectPairTy ClassSubobjectPair(RD, Base); 1450b57cec5SDimitry Andric 146*0fca6ea1SDimitry Andric SubVTTIndicesMapTy::iterator I = SubVTTIndices.find(ClassSubobjectPair); 147*0fca6ea1SDimitry Andric if (I != SubVTTIndices.end()) 1480b57cec5SDimitry Andric return I->second; 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 1510b57cec5SDimitry Andric 152*0fca6ea1SDimitry Andric for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator 153*0fca6ea1SDimitry Andric I = Builder.getSubVTTIndices().begin(), 154*0fca6ea1SDimitry Andric E = Builder.getSubVTTIndices().end(); 155*0fca6ea1SDimitry Andric I != E; ++I) { 1560b57cec5SDimitry Andric // Insert all indices. 1570b57cec5SDimitry Andric BaseSubobjectPairTy ClassSubobjectPair(RD, I->first); 1580b57cec5SDimitry Andric 159*0fca6ea1SDimitry Andric SubVTTIndices.insert(std::make_pair(ClassSubobjectPair, I->second)); 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 162*0fca6ea1SDimitry Andric I = SubVTTIndices.find(ClassSubobjectPair); 163*0fca6ea1SDimitry Andric assert(I != SubVTTIndices.end() && "Did not find index!"); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric return I->second; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric uint64_t 1690b57cec5SDimitry Andric CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 1700b57cec5SDimitry Andric BaseSubobject Base) { 1710b57cec5SDimitry Andric SecondaryVirtualPointerIndicesMapTy::iterator I = 1720b57cec5SDimitry Andric SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric if (I != SecondaryVirtualPointerIndices.end()) 1750b57cec5SDimitry Andric return I->second; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric // Insert all secondary vpointer indices. 1800b57cec5SDimitry Andric for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = 1810b57cec5SDimitry Andric Builder.getSecondaryVirtualPointerIndices().begin(), 1820b57cec5SDimitry Andric E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) { 1830b57cec5SDimitry Andric std::pair<const CXXRecordDecl *, BaseSubobject> Pair = 1840b57cec5SDimitry Andric std::make_pair(RD, I->first); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second)); 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 1900b57cec5SDimitry Andric assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!"); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric return I->second; 1930b57cec5SDimitry Andric } 194