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"
17using namespace clang;
18using namespace CodeGen;
19
20static llvm::GlobalVariable *
21GetAddrOfVTTVTable(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(RD: MostDerivedClass, VPtrOffset: CharUnits());
31 }
32
33 return CGVT.GenerateConstructionVTable(RD: MostDerivedClass,
34 Base: VTable.getBaseSubobject(),
35 BaseIsVirtual: VTable.isVirtual(),
36 Linkage,
37 AddressPoints);
38}
39
40void
41CodeGenVTables::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 ElementType: CGM.GlobalsInt8PtrTy, NumElements: 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(Elt: VTableAddressPointsMapTy());
53 VTables.push_back(Elt: GetAddrOfVTTVTable(CGVT&: *this, CGM, MostDerivedClass: RD, VTable: *i, Linkage,
54 AddressPoints&: 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 Base: i->VTableBase);
68 } else {
69 AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(Val: 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(Ty: CGM.Int32Ty, V: 0),
76 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: AddressPoint.VTableIndex),
77 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: 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(Ty: getVTableComponentType());
84 unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize(
85 Ty: cast<llvm::StructType>(Val: VTable->getValueType())
86 ->getElementType(N: AddressPoint.VTableIndex));
87 unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
88 llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true),
89 llvm::APInt(32, VTableSize - Offset, true));
90 llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
91 Ty: VTable->getValueType(), C: VTable, IdxList: Idxs, /*InBounds=*/NW: true, InRange);
92
93 if (const auto &Schema =
94 CGM.getCodeGenOpts().PointerAuth.CXXVTTVTablePointers)
95 Init = CGM.getConstantSignedPointer(Pointer: Init, Schema, StorageAddress: nullptr, SchemaDecl: GlobalDecl(),
96 SchemaType: QualType());
97
98 VTTComponents.push_back(Elt: Init);
99 }
100
101 llvm::Constant *Init = llvm::ConstantArray::get(T: ArrayType, V: VTTComponents);
102
103 VTT->setInitializer(Init);
104
105 // Set the correct linkage.
106 VTT->setLinkage(Linkage);
107
108 if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
109 VTT->setComdat(CGM.getModule().getOrInsertComdat(Name: VTT->getName()));
110
111 // Set the visibility. This will already have been set on the VTT declaration.
112 // Set it again, now that we have a definition, as the implicit visibility can
113 // apply differently to definitions.
114 CGM.setGVProperties(GV: VTT, D: RD);
115}
116
117llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
118 assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
119
120 SmallString<256> OutName;
121 llvm::raw_svector_ostream Out(OutName);
122 cast<ItaniumMangleContext>(Val&: CGM.getCXXABI().getMangleContext())
123 .mangleCXXVTT(RD, Out);
124 StringRef Name = OutName.str();
125
126 // This will also defer the definition of the VTT.
127 (void) CGM.getCXXABI().getAddrOfVTable(RD, VPtrOffset: CharUnits());
128
129 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
130
131 llvm::ArrayType *ArrayType = llvm::ArrayType::get(
132 ElementType: CGM.GlobalsInt8PtrTy, NumElements: Builder.getVTTComponents().size());
133 llvm::Align Align = CGM.getDataLayout().getABITypeAlign(Ty: CGM.GlobalsInt8PtrTy);
134
135 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
136 Name, Ty: ArrayType, Linkage: llvm::GlobalValue::ExternalLinkage, Alignment: Align);
137 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
138 CGM.setGVProperties(GV, D: RD);
139 return GV;
140}
141
142uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
143 BaseSubobject Base) {
144 BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
145
146 SubVTTIndicesMapTy::iterator I = SubVTTIndices.find(Val: ClassSubobjectPair);
147 if (I != SubVTTIndices.end())
148 return I->second;
149
150 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
151
152 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator
153 I = Builder.getSubVTTIndices().begin(),
154 E = Builder.getSubVTTIndices().end();
155 I != E; ++I) {
156 // Insert all indices.
157 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
158
159 SubVTTIndices.insert(KV: std::make_pair(x&: ClassSubobjectPair, y: I->second));
160 }
161
162 I = SubVTTIndices.find(Val: ClassSubobjectPair);
163 assert(I != SubVTTIndices.end() && "Did not find index!");
164
165 return I->second;
166}
167
168uint64_t
169CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
170 BaseSubobject Base) {
171 SecondaryVirtualPointerIndicesMapTy::iterator I =
172 SecondaryVirtualPointerIndices.find(Val: std::make_pair(x&: RD, y&: Base));
173
174 if (I != SecondaryVirtualPointerIndices.end())
175 return I->second;
176
177 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
178
179 // Insert all secondary vpointer indices.
180 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
181 Builder.getSecondaryVirtualPointerIndices().begin(),
182 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
183 std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
184 std::make_pair(x&: RD, y: I->first);
185
186 SecondaryVirtualPointerIndices.insert(KV: std::make_pair(x&: Pair, y: I->second));
187 }
188
189 I = SecondaryVirtualPointerIndices.find(Val: std::make_pair(x&: RD, y&: Base));
190 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
191
192 return I->second;
193}
194