1//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
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 virtual tables.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGCXXABI.h"
14#include "CGDebugInfo.h"
15#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
17#include "clang/AST/Attr.h"
18#include "clang/AST/CXXInheritance.h"
19#include "clang/AST/RecordLayout.h"
20#include "clang/Basic/CodeGenOptions.h"
21#include "clang/CodeGen/CGFunctionInfo.h"
22#include "clang/CodeGen/ConstantInitBuilder.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/Transforms/Utils/Cloning.h"
25#include <algorithm>
26#include <cstdio>
27#include <utility>
28
29using namespace clang;
30using namespace CodeGen;
31
32CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
33 : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
34
35llvm::Constant *CodeGenModule::GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
36 GlobalDecl GD) {
37 return GetOrCreateLLVMFunction(MangledName: Name, Ty: FnTy, D: GD, /*ForVTable=*/true,
38 /*DontDefer=*/true, /*IsThunk=*/true);
39}
40
41llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
42 llvm::GlobalVariable *VTable =
43 CGM.getCXXABI().getAddrOfVTable(RD, VPtrOffset: CharUnits());
44 return VTable;
45}
46
47static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
48 llvm::Function *ThunkFn, bool ForVTable,
49 GlobalDecl GD) {
50 CGM.setFunctionLinkage(GD, F: ThunkFn);
51 CGM.getCXXABI().setThunkLinkage(Thunk: ThunkFn, ForVTable, GD,
52 ReturnAdjustment: !Thunk.Return.isEmpty());
53
54 // Set the right visibility.
55 CGM.setGVProperties(GV: ThunkFn, GD);
56
57 if (!CGM.getCXXABI().exportThunk()) {
58 ThunkFn->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
59 ThunkFn->setDSOLocal(true);
60 }
61
62 if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
63 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(Name: ThunkFn->getName()));
64}
65
66#ifndef NDEBUG
67static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
68 const ABIArgInfo &infoR, CanQualType typeR) {
69 return (infoL.getKind() == infoR.getKind() &&
70 (typeL == typeR ||
71 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
72 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
73}
74#endif
75
76static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
77 QualType ResultType, RValue RV,
78 const ThunkInfo &Thunk) {
79 // Emit the return adjustment.
80 bool NullCheckValue = !ResultType->isReferenceType();
81
82 llvm::BasicBlock *AdjustNull = nullptr;
83 llvm::BasicBlock *AdjustNotNull = nullptr;
84 llvm::BasicBlock *AdjustEnd = nullptr;
85
86 llvm::Value *ReturnValue = RV.getScalarVal();
87
88 if (NullCheckValue) {
89 AdjustNull = CGF.createBasicBlock(name: "adjust.null");
90 AdjustNotNull = CGF.createBasicBlock(name: "adjust.notnull");
91 AdjustEnd = CGF.createBasicBlock(name: "adjust.end");
92
93 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Arg: ReturnValue);
94 CGF.Builder.CreateCondBr(Cond: IsNull, True: AdjustNull, False: AdjustNotNull);
95 CGF.EmitBlock(BB: AdjustNotNull);
96 }
97
98 auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
99 auto ClassAlign = CGF.CGM.getClassPointerAlignment(CD: ClassDecl);
100 ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(
101 CGF,
102 Ret: Address(ReturnValue, CGF.ConvertTypeForMem(T: ResultType->getPointeeType()),
103 ClassAlign),
104 UnadjustedClass: ClassDecl, RA: Thunk.Return);
105
106 if (NullCheckValue) {
107 CGF.Builder.CreateBr(Dest: AdjustEnd);
108 CGF.EmitBlock(BB: AdjustNull);
109 CGF.Builder.CreateBr(Dest: AdjustEnd);
110 CGF.EmitBlock(BB: AdjustEnd);
111
112 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Ty: ReturnValue->getType(), NumReservedValues: 2);
113 PHI->addIncoming(V: ReturnValue, BB: AdjustNotNull);
114 PHI->addIncoming(V: llvm::Constant::getNullValue(Ty: ReturnValue->getType()),
115 BB: AdjustNull);
116 ReturnValue = PHI;
117 }
118
119 return RValue::get(V: ReturnValue);
120}
121
122/// This function clones a function's DISubprogram node and enters it into
123/// a value map with the intent that the map can be utilized by the cloner
124/// to short-circuit Metadata node mapping.
125/// Furthermore, the function resolves any DILocalVariable nodes referenced
126/// by dbg.value intrinsics so they can be properly mapped during cloning.
127static void resolveTopLevelMetadata(llvm::Function *Fn,
128 llvm::ValueToValueMapTy &VMap) {
129 // Clone the DISubprogram node and put it into the Value map.
130 auto *DIS = Fn->getSubprogram();
131 if (!DIS)
132 return;
133 auto *NewDIS = llvm::MDNode::replaceWithDistinct(N: DIS->clone());
134 // As DISubprogram remapping is avoided, clear retained nodes list of
135 // cloned DISubprogram from retained nodes local to original DISubprogram.
136 // FIXME: Thunk function signature is produced wrong in DWARF, as retained
137 // nodes are not remapped.
138 NewDIS->replaceRetainedNodes(N: llvm::MDTuple::get(Context&: Fn->getContext(), MDs: {}));
139 VMap.MD()[DIS].reset(MD: NewDIS);
140
141 // Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes
142 // they are referencing.
143 //
144 // DIDerivedTypes referring to incomplete Clang types, or
145 // LLVM enumeration types representing complete enums with no definition
146 // may be still unresolved. As they can't be cloned, keep references
147 // to the types from the base subprogram.
148 // FIXME: As a result, variables of cloned subprogram may refer to local types
149 // from base subprogram. In such case, type locality information is damaged.
150 // Find a way to enable cloning of all local types.
151 auto PrepareVariableMapping = [&VMap](llvm::DILocalVariable *DILocal) {
152 if (DILocal->isResolved())
153 return;
154
155 if (llvm::DIType *Ty = DILocal->getType(); Ty && !Ty->isResolved())
156 VMap.MD()[Ty].reset(MD: Ty);
157
158 DILocal->resolve();
159 };
160
161 for (auto &BB : *Fn) {
162 for (auto &I : BB) {
163 for (llvm::DbgVariableRecord &DVR :
164 llvm::filterDbgVars(R: I.getDbgRecordRange()))
165 PrepareVariableMapping(DVR.getVariable());
166
167 if (auto *DII = dyn_cast<llvm::DbgVariableIntrinsic>(Val: &I))
168 PrepareVariableMapping(DII->getVariable());
169 }
170 }
171}
172
173// This function does roughly the same thing as GenerateThunk, but in a
174// very different way, so that va_start and va_end work correctly.
175// FIXME: This function assumes "this" is the first non-sret LLVM argument of
176// a function, and that there is an alloca built in the entry block
177// for all accesses to "this".
178// FIXME: This function assumes there is only one "ret" statement per function.
179// FIXME: Cloning isn't correct in the presence of indirect goto!
180// FIXME: This implementation of thunks bloats codesize by duplicating the
181// function definition. There are alternatives:
182// 1. Add some sort of stub support to LLVM for cases where we can
183// do a this adjustment, then a sibcall.
184// 2. We could transform the definition to take a va_list instead of an
185// actual variable argument list, then have the thunks (including a
186// no-op thunk for the regular definition) call va_start/va_end.
187// There's a bit of per-call overhead for this solution, but it's
188// better for codesize if the definition is long.
189llvm::Function *
190CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn,
191 const CGFunctionInfo &FnInfo,
192 GlobalDecl GD, const ThunkInfo &Thunk) {
193 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
194 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
195 QualType ResultType = FPT->getReturnType();
196
197 // Get the original function
198 assert(FnInfo.isVariadic());
199 llvm::Type *Ty = CGM.getTypes().GetFunctionType(Info: FnInfo);
200 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
201 llvm::Function *BaseFn = cast<llvm::Function>(Val: Callee);
202
203 // Cloning can't work if we don't have a definition. The Microsoft ABI may
204 // require thunks when a definition is not available. Emit an error in these
205 // cases.
206 if (!MD->isDefined()) {
207 CGM.ErrorUnsupported(D: MD, Type: "return-adjusting thunk with variadic arguments");
208 return Fn;
209 }
210 assert(!BaseFn->isDeclaration() && "cannot clone undefined variadic method");
211
212 // Clone to thunk.
213 llvm::ValueToValueMapTy VMap;
214
215 // We are cloning a function while some Metadata nodes are still unresolved.
216 // Ensure that the value mapper does not encounter any of them.
217 resolveTopLevelMetadata(Fn: BaseFn, VMap);
218 llvm::Function *NewFn = llvm::CloneFunction(F: BaseFn, VMap);
219 Fn->replaceAllUsesWith(V: NewFn);
220 NewFn->takeName(V: Fn);
221 Fn->eraseFromParent();
222 Fn = NewFn;
223
224 // "Initialize" CGF (minimally).
225 CurFn = Fn;
226
227 // Get the "this" value
228 llvm::Function::arg_iterator AI = Fn->arg_begin();
229 if (CGM.ReturnTypeUsesSRet(FI: FnInfo))
230 ++AI;
231
232 // Find the first store of "this", which will be to the alloca associated
233 // with "this".
234 Address ThisPtr = makeNaturalAddressForPointer(
235 Ptr: &*AI, T: MD->getFunctionObjectParameterType(),
236 Alignment: CGM.getClassPointerAlignment(CD: MD->getParent()));
237 llvm::BasicBlock *EntryBB = &Fn->front();
238 llvm::BasicBlock::iterator ThisStore =
239 llvm::find_if(Range&: *EntryBB, P: [&](llvm::Instruction &I) {
240 return isa<llvm::StoreInst>(Val: I) && I.getOperand(i: 0) == &*AI;
241 });
242 assert(ThisStore != EntryBB->end() &&
243 "Store of this should be in entry block?");
244 // Adjust "this", if necessary.
245 Builder.SetInsertPoint(&*ThisStore);
246
247 const CXXRecordDecl *ThisValueClass = Thunk.ThisType->getPointeeCXXRecordDecl();
248 llvm::Value *AdjustedThisPtr = CGM.getCXXABI().performThisAdjustment(
249 CGF&: *this, This: ThisPtr, UnadjustedClass: ThisValueClass, TI: Thunk);
250 AdjustedThisPtr = Builder.CreateBitCast(V: AdjustedThisPtr,
251 DestTy: ThisStore->getOperand(i: 0)->getType());
252 ThisStore->setOperand(i: 0, Val: AdjustedThisPtr);
253
254 if (!Thunk.Return.isEmpty()) {
255 // Fix up the returned value, if necessary.
256 for (llvm::BasicBlock &BB : *Fn) {
257 llvm::Instruction *T = BB.getTerminator();
258 if (isa<llvm::ReturnInst>(Val: T)) {
259 RValue RV = RValue::get(V: T->getOperand(i: 0));
260 T->eraseFromParent();
261 Builder.SetInsertPoint(&BB);
262 RV = PerformReturnAdjustment(CGF&: *this, ResultType, RV, Thunk);
263 Builder.CreateRet(V: RV.getScalarVal());
264 break;
265 }
266 }
267 }
268
269 return Fn;
270}
271
272void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
273 const CGFunctionInfo &FnInfo,
274 bool IsUnprototyped) {
275 assert(!CurGD.getDecl() && "CurGD was already set!");
276 CurGD = GD;
277 CurFuncIsThunk = true;
278
279 // Build FunctionArgs.
280 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
281 QualType ThisType = MD->getThisType();
282 QualType ResultType;
283 if (IsUnprototyped)
284 ResultType = CGM.getContext().VoidTy;
285 else if (CGM.getCXXABI().HasThisReturn(GD))
286 ResultType = ThisType;
287 else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
288 ResultType = CGM.getContext().VoidPtrTy;
289 else
290 ResultType = MD->getType()->castAs<FunctionProtoType>()->getReturnType();
291 FunctionArgList FunctionArgs;
292
293 // Create the implicit 'this' parameter declaration.
294 CGM.getCXXABI().buildThisParam(CGF&: *this, Params&: FunctionArgs);
295
296 // Add the rest of the parameters, if we have a prototype to work with.
297 if (!IsUnprototyped) {
298 FunctionArgs.append(in_start: MD->param_begin(), in_end: MD->param_end());
299
300 if (isa<CXXDestructorDecl>(Val: MD))
301 CGM.getCXXABI().addImplicitStructorParams(CGF&: *this, ResTy&: ResultType,
302 Params&: FunctionArgs);
303 }
304
305 // Start defining the function.
306 auto NL = ApplyDebugLocation::CreateEmpty(CGF&: *this);
307 StartFunction(GD: GlobalDecl(), RetTy: ResultType, Fn, FnInfo, Args: FunctionArgs,
308 Loc: MD->getLocation());
309 // Create a scope with an artificial location for the body of this function.
310 auto AL = ApplyDebugLocation::CreateArtificial(CGF&: *this);
311
312 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
313 CGM.getCXXABI().EmitInstanceFunctionProlog(CGF&: *this);
314 CXXThisValue = CXXABIThisValue;
315 CurCodeDecl = MD;
316 CurFuncDecl = MD;
317}
318
319void CodeGenFunction::FinishThunk() {
320 // Clear these to restore the invariants expected by
321 // StartFunction/FinishFunction.
322 CurCodeDecl = nullptr;
323 CurFuncDecl = nullptr;
324
325 FinishFunction();
326}
327
328void CodeGenFunction::EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
329 const ThunkInfo *Thunk,
330 bool IsUnprototyped) {
331 assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
332 "Please use a new CGF for this thunk");
333 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: CurGD.getDecl());
334
335 // Adjust the 'this' pointer if necessary
336 const CXXRecordDecl *ThisValueClass =
337 MD->getThisType()->getPointeeCXXRecordDecl();
338 if (Thunk)
339 ThisValueClass = Thunk->ThisType->getPointeeCXXRecordDecl();
340
341 llvm::Value *AdjustedThisPtr =
342 Thunk ? CGM.getCXXABI().performThisAdjustment(CGF&: *this, This: LoadCXXThisAddress(),
343 UnadjustedClass: ThisValueClass, TI: *Thunk)
344 : LoadCXXThis();
345
346 // If perfect forwarding is required a variadic method, a method using
347 // inalloca, or an unprototyped thunk, use musttail. Emit an error if this
348 // thunk requires a return adjustment, since that is impossible with musttail.
349 if (CurFnInfo->usesInAlloca() || CurFnInfo->isVariadic() || IsUnprototyped) {
350 if (Thunk && !Thunk->Return.isEmpty()) {
351 if (IsUnprototyped)
352 CGM.ErrorUnsupported(
353 D: MD, Type: "return-adjusting thunk with incomplete parameter type");
354 else if (CurFnInfo->isVariadic())
355 llvm_unreachable("shouldn't try to emit musttail return-adjusting "
356 "thunks for variadic functions");
357 else
358 CGM.ErrorUnsupported(
359 D: MD, Type: "non-trivial argument copy for return-adjusting thunk");
360 }
361 EmitMustTailThunk(GD: CurGD, AdjustedThisPtr, Callee);
362 return;
363 }
364
365 // Start building CallArgs.
366 CallArgList CallArgs;
367 QualType ThisType = MD->getThisType();
368 CallArgs.add(rvalue: RValue::get(V: AdjustedThisPtr), type: ThisType);
369
370 if (isa<CXXDestructorDecl>(Val: MD))
371 CGM.getCXXABI().adjustCallArgsForDestructorThunk(CGF&: *this, GD: CurGD, CallArgs);
372
373#ifndef NDEBUG
374 unsigned PrefixArgs = CallArgs.size() - 1;
375#endif
376 // Add the rest of the arguments.
377 for (const ParmVarDecl *PD : MD->parameters())
378 EmitDelegateCallArg(args&: CallArgs, param: PD, loc: SourceLocation());
379
380 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
381
382#ifndef NDEBUG
383 const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall(
384 CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1), PrefixArgs);
385 assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
386 CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
387 CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
388 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
389 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
390 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
391 assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
392 for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
393 assert(similar(CallFnInfo.arg_begin()[i].info,
394 CallFnInfo.arg_begin()[i].type,
395 CurFnInfo->arg_begin()[i].info,
396 CurFnInfo->arg_begin()[i].type));
397#endif
398
399 // Determine whether we have a return value slot to use.
400 QualType ResultType = CGM.getCXXABI().HasThisReturn(GD: CurGD)
401 ? ThisType
402 : CGM.getCXXABI().hasMostDerivedReturn(GD: CurGD)
403 ? CGM.getContext().VoidPtrTy
404 : FPT->getReturnType();
405 ReturnValueSlot Slot;
406 if (!ResultType->isVoidType() &&
407 (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect ||
408 hasAggregateEvaluationKind(T: ResultType)))
409 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified(),
410 /*IsUnused=*/false, /*IsExternallyDestructed=*/true);
411
412 // Now emit our call.
413 llvm::CallBase *CallOrInvoke;
414 RValue RV = EmitCall(CallInfo: *CurFnInfo, Callee: CGCallee::forDirect(functionPtr: Callee, abstractInfo: CurGD), ReturnValue: Slot,
415 Args: CallArgs, CallOrInvoke: &CallOrInvoke);
416
417 // Consider return adjustment if we have ThunkInfo.
418 if (Thunk && !Thunk->Return.isEmpty())
419 RV = PerformReturnAdjustment(CGF&: *this, ResultType, RV, Thunk: *Thunk);
420 else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(Val: CallOrInvoke))
421 Call->setTailCallKind(llvm::CallInst::TCK_Tail);
422
423 // Emit return.
424 if (!ResultType->isVoidType() && Slot.isNull())
425 CGM.getCXXABI().EmitReturnFromThunk(CGF&: *this, RV, ResultType);
426
427 // Disable the final ARC autorelease.
428 AutoreleaseResult = false;
429
430 FinishThunk();
431}
432
433void CodeGenFunction::EmitMustTailThunk(GlobalDecl GD,
434 llvm::Value *AdjustedThisPtr,
435 llvm::FunctionCallee Callee) {
436 // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
437 // to translate AST arguments into LLVM IR arguments. For thunks, we know
438 // that the caller prototype more or less matches the callee prototype with
439 // the exception of 'this'.
440 SmallVector<llvm::Value *, 8> Args(llvm::make_pointer_range(Range: CurFn->args()));
441
442 // Set the adjusted 'this' pointer.
443 const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
444 if (ThisAI.isDirect()) {
445 const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
446 int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
447 llvm::Type *ThisType = Args[ThisArgNo]->getType();
448 if (ThisType != AdjustedThisPtr->getType())
449 AdjustedThisPtr = Builder.CreateBitCast(V: AdjustedThisPtr, DestTy: ThisType);
450 Args[ThisArgNo] = AdjustedThisPtr;
451 } else {
452 assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
453 Address ThisAddr = GetAddrOfLocalVar(VD: CXXABIThisDecl);
454 llvm::Type *ThisType = ThisAddr.getElementType();
455 if (ThisType != AdjustedThisPtr->getType())
456 AdjustedThisPtr = Builder.CreateBitCast(V: AdjustedThisPtr, DestTy: ThisType);
457 Builder.CreateStore(Val: AdjustedThisPtr, Addr: ThisAddr);
458 }
459
460 // Emit the musttail call manually. Even if the prologue pushed cleanups, we
461 // don't actually want to run them.
462 llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
463 Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
464
465 // Apply the standard set of call attributes.
466 unsigned CallingConv;
467 llvm::AttributeList Attrs;
468 CGM.ConstructAttributeList(Name: Callee.getCallee()->getName(), Info: *CurFnInfo, CalleeInfo: GD,
469 Attrs, CallingConv, /*AttrOnCallSite=*/true,
470 /*IsThunk=*/false);
471 Call->setAttributes(Attrs);
472 Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
473
474 if (Call->getType()->isVoidTy())
475 Builder.CreateRetVoid();
476 else
477 Builder.CreateRet(V: Call);
478
479 // Finish the function to maintain CodeGenFunction invariants.
480 // FIXME: Don't emit unreachable code.
481 EmitBlock(BB: createBasicBlock());
482
483 FinishThunk();
484}
485
486void CodeGenFunction::generateThunk(llvm::Function *Fn,
487 const CGFunctionInfo &FnInfo, GlobalDecl GD,
488 const ThunkInfo &Thunk,
489 bool IsUnprototyped) {
490 StartThunk(Fn, GD, FnInfo, IsUnprototyped);
491 // Create a scope with an artificial location for the body of this function.
492 auto AL = ApplyDebugLocation::CreateArtificial(CGF&: *this);
493
494 // Get our callee. Use a placeholder type if this method is unprototyped so
495 // that CodeGenModule doesn't try to set attributes.
496 llvm::Type *Ty;
497 if (IsUnprototyped)
498 Ty = llvm::StructType::get(Context&: getLLVMContext());
499 else
500 Ty = CGM.getTypes().GetFunctionType(Info: FnInfo);
501
502 llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
503
504 // Make the call and return the result.
505 EmitCallAndReturnForThunk(Callee: llvm::FunctionCallee(Fn->getFunctionType(), Callee),
506 Thunk: &Thunk, IsUnprototyped);
507}
508
509static bool shouldEmitVTableThunk(CodeGenModule &CGM, const CXXMethodDecl *MD,
510 bool IsUnprototyped, bool ForVTable) {
511 // Always emit thunks in the MS C++ ABI. We cannot rely on other TUs to
512 // provide thunks for us.
513 if (CGM.getTarget().getCXXABI().isMicrosoft())
514 return true;
515
516 // In the Itanium C++ ABI, vtable thunks are provided by TUs that provide
517 // definitions of the main method. Therefore, emitting thunks with the vtable
518 // is purely an optimization. Emit the thunk if optimizations are enabled and
519 // all of the parameter types are complete.
520 if (ForVTable)
521 return CGM.getCodeGenOpts().OptimizationLevel && !IsUnprototyped;
522
523 // Always emit thunks along with the method definition.
524 return true;
525}
526
527llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
528 const ThunkInfo &TI,
529 bool ForVTable) {
530 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
531
532 // First, get a declaration. Compute the mangled name. Don't worry about
533 // getting the function prototype right, since we may only need this
534 // declaration to fill in a vtable slot.
535 SmallString<256> Name;
536 MangleContext &MCtx = CGM.getCXXABI().getMangleContext();
537 llvm::raw_svector_ostream Out(Name);
538
539 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Val: MD)) {
540 MCtx.mangleCXXDtorThunk(DD, Type: GD.getDtorType(), Thunk: TI,
541 /* elideOverrideInfo */ ElideOverrideInfo: false, Out);
542 } else
543 MCtx.mangleThunk(MD, Thunk: TI, /* elideOverrideInfo */ ElideOverrideInfo: false, Out);
544
545 if (CGM.getContext().useAbbreviatedThunkName(VirtualMethodDecl: GD, MangledName: Name.str())) {
546 Name = "";
547 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Val: MD))
548 MCtx.mangleCXXDtorThunk(DD, Type: GD.getDtorType(), Thunk: TI,
549 /* elideOverrideInfo */ ElideOverrideInfo: true, Out);
550 else
551 MCtx.mangleThunk(MD, Thunk: TI, /* elideOverrideInfo */ ElideOverrideInfo: true, Out);
552 }
553
554 llvm::Type *ThunkVTableTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
555 llvm::Constant *Thunk = CGM.GetAddrOfThunk(Name, FnTy: ThunkVTableTy, GD);
556
557 // If we don't need to emit a definition, return this declaration as is.
558 bool IsUnprototyped = !CGM.getTypes().isFuncTypeConvertible(
559 FT: MD->getType()->castAs<FunctionType>());
560 if (!shouldEmitVTableThunk(CGM, MD, IsUnprototyped, ForVTable))
561 return Thunk;
562
563 // Arrange a function prototype appropriate for a function definition. In some
564 // cases in the MS ABI, we may need to build an unprototyped musttail thunk.
565 const CGFunctionInfo &FnInfo =
566 IsUnprototyped ? CGM.getTypes().arrangeUnprototypedMustTailThunk(MD)
567 : CGM.getTypes().arrangeGlobalDeclaration(GD);
568 llvm::FunctionType *ThunkFnTy = CGM.getTypes().GetFunctionType(Info: FnInfo);
569
570 // If the type of the underlying GlobalValue is wrong, we'll have to replace
571 // it. It should be a declaration.
572 llvm::Function *ThunkFn = cast<llvm::Function>(Val: Thunk->stripPointerCasts());
573 if (ThunkFn->getFunctionType() != ThunkFnTy) {
574 llvm::GlobalValue *OldThunkFn = ThunkFn;
575
576 assert(OldThunkFn->isDeclaration() && "Shouldn't replace non-declaration");
577
578 // Remove the name from the old thunk function and get a new thunk.
579 OldThunkFn->setName(StringRef());
580 ThunkFn = llvm::Function::Create(Ty: ThunkFnTy, Linkage: llvm::Function::ExternalLinkage,
581 N: Name.str(), M: &CGM.getModule());
582 CGM.SetLLVMFunctionAttributes(GD: MD, Info: FnInfo, F: ThunkFn, /*IsThunk=*/false);
583
584 if (!OldThunkFn->use_empty()) {
585 OldThunkFn->replaceAllUsesWith(V: ThunkFn);
586 }
587
588 // Remove the old thunk.
589 OldThunkFn->eraseFromParent();
590 }
591
592 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
593 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
594
595 if (!ThunkFn->isDeclaration()) {
596 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
597 // There is already a thunk emitted for this function, do nothing.
598 return ThunkFn;
599 }
600
601 setThunkProperties(CGM, Thunk: TI, ThunkFn, ForVTable, GD);
602 return ThunkFn;
603 }
604
605 // If this will be unprototyped, add the "thunk" attribute so that LLVM knows
606 // that the return type is meaningless. These thunks can be used to call
607 // functions with differing return types, and the caller is required to cast
608 // the prototype appropriately to extract the correct value.
609 if (IsUnprototyped)
610 ThunkFn->addFnAttr(Kind: "thunk");
611
612 CGM.SetLLVMFunctionAttributesForDefinition(D: GD.getDecl(), F: ThunkFn);
613
614 // Thunks for variadic methods are special because in general variadic
615 // arguments cannot be perfectly forwarded. In the general case, clang
616 // implements such thunks by cloning the original function body. However, for
617 // thunks with no return adjustment on targets that support musttail, we can
618 // use musttail to perfectly forward the variadic arguments.
619 bool ShouldCloneVarArgs = false;
620 if (!IsUnprototyped && ThunkFn->isVarArg()) {
621 ShouldCloneVarArgs = true;
622 if (TI.Return.isEmpty()) {
623 switch (CGM.getTriple().getArch()) {
624 case llvm::Triple::x86_64:
625 case llvm::Triple::x86:
626 case llvm::Triple::aarch64:
627 ShouldCloneVarArgs = false;
628 break;
629 default:
630 break;
631 }
632 }
633 }
634
635 if (ShouldCloneVarArgs) {
636 if (UseAvailableExternallyLinkage)
637 return ThunkFn;
638 ThunkFn =
639 CodeGenFunction(CGM).GenerateVarArgsThunk(Fn: ThunkFn, FnInfo, GD, Thunk: TI);
640 } else {
641 // Normal thunk body generation.
642 CodeGenFunction(CGM).generateThunk(Fn: ThunkFn, FnInfo, GD, Thunk: TI, IsUnprototyped);
643 }
644
645 setThunkProperties(CGM, Thunk: TI, ThunkFn, ForVTable, GD);
646 return ThunkFn;
647}
648
649void CodeGenVTables::EmitThunks(GlobalDecl GD) {
650 const CXXMethodDecl *MD =
651 cast<CXXMethodDecl>(Val: GD.getDecl())->getCanonicalDecl();
652
653 // We don't need to generate thunks for the base destructor.
654 if (isa<CXXDestructorDecl>(Val: MD) && GD.getDtorType() == Dtor_Base)
655 return;
656
657 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
658 VTContext->getThunkInfo(GD);
659
660 if (!ThunkInfoVector)
661 return;
662
663 for (const ThunkInfo& Thunk : *ThunkInfoVector)
664 maybeEmitThunk(GD, TI: Thunk, /*ForVTable=*/false);
665}
666
667void CodeGenVTables::addRelativeComponent(ConstantArrayBuilder &builder,
668 llvm::Constant *component,
669 unsigned vtableAddressPoint,
670 bool vtableHasLocalLinkage,
671 bool isCompleteDtor) const {
672 // No need to get the offset of a nullptr.
673 if (component->isNullValue())
674 return builder.add(value: llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: 0));
675
676 auto *globalVal =
677 cast<llvm::GlobalValue>(Val: component->stripPointerCastsAndAliases());
678 llvm::Module &module = CGM.getModule();
679
680 // We don't want to copy the linkage of the vtable exactly because we still
681 // want the stub/proxy to be emitted for properly calculating the offset.
682 // Examples where there would be no symbol emitted are available_externally
683 // and private linkages.
684 //
685 // `internal` linkage results in STB_LOCAL Elf binding while still manifesting a
686 // local symbol.
687 //
688 // `linkonce_odr` linkage results in a STB_DEFAULT Elf binding but also allows for
689 // the rtti_proxy to be transparently replaced with a GOTPCREL reloc by a
690 // target that supports this replacement.
691 auto stubLinkage = vtableHasLocalLinkage
692 ? llvm::GlobalValue::InternalLinkage
693 : llvm::GlobalValue::LinkOnceODRLinkage;
694
695 llvm::Constant *target;
696 if (auto *func = dyn_cast<llvm::Function>(Val: globalVal)) {
697 target = llvm::DSOLocalEquivalent::get(GV: func);
698 } else {
699 llvm::SmallString<16> rttiProxyName(globalVal->getName());
700 rttiProxyName.append(RHS: ".rtti_proxy");
701
702 // The RTTI component may not always be emitted in the same linkage unit as
703 // the vtable. As a general case, we can make a dso_local proxy to the RTTI
704 // that points to the actual RTTI struct somewhere. This will result in a
705 // GOTPCREL relocation when taking the relative offset to the proxy.
706 llvm::GlobalVariable *proxy = module.getNamedGlobal(Name: rttiProxyName);
707 if (!proxy) {
708 proxy = new llvm::GlobalVariable(module, globalVal->getType(),
709 /*isConstant=*/true, stubLinkage,
710 globalVal, rttiProxyName);
711 proxy->setDSOLocal(true);
712 proxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
713 if (!proxy->hasLocalLinkage()) {
714 proxy->setVisibility(llvm::GlobalValue::HiddenVisibility);
715 proxy->setComdat(module.getOrInsertComdat(Name: rttiProxyName));
716 }
717 // Do not instrument the rtti proxies with hwasan to avoid a duplicate
718 // symbol error. Aliases generated by hwasan will retain the same namebut
719 // the addresses they are set to may have different tags from different
720 // compilation units. We don't run into this without hwasan because the
721 // proxies are in comdat groups, but those aren't propagated to the alias.
722 RemoveHwasanMetadata(GV: proxy);
723 }
724 target = proxy;
725 }
726
727 builder.addRelativeOffsetToPosition(type: CGM.Int32Ty, target,
728 /*position=*/vtableAddressPoint);
729}
730
731llvm::Type *CodeGenModule::getVTableComponentType() const {
732 if (getLangOpts().RelativeCXXABIVTables)
733 return Int32Ty;
734 return GlobalsInt8PtrTy;
735}
736
737llvm::Type *CodeGenVTables::getVTableComponentType() const {
738 return CGM.getVTableComponentType();
739}
740
741static void AddPointerLayoutOffset(const CodeGenModule &CGM,
742 ConstantArrayBuilder &builder,
743 CharUnits offset) {
744 builder.add(value: llvm::ConstantExpr::getIntToPtr(
745 C: llvm::ConstantInt::getSigned(Ty: CGM.PtrDiffTy, V: offset.getQuantity()),
746 Ty: CGM.GlobalsInt8PtrTy));
747}
748
749static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
750 ConstantArrayBuilder &builder,
751 CharUnits offset) {
752 builder.add(value: llvm::ConstantInt::getSigned(Ty: CGM.Int32Ty, V: offset.getQuantity()));
753}
754
755void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
756 const VTableLayout &layout,
757 unsigned componentIndex,
758 llvm::Constant *rtti,
759 unsigned &nextVTableThunkIndex,
760 unsigned vtableAddressPoint,
761 bool vtableHasLocalLinkage) {
762 auto &component = layout.vtable_components()[componentIndex];
763
764 bool RelativeCXXABIVTables = CGM.getLangOpts().RelativeCXXABIVTables;
765 auto addOffsetConstant =
766 RelativeCXXABIVTables ? AddRelativeLayoutOffset : AddPointerLayoutOffset;
767
768 switch (component.getKind()) {
769 case VTableComponent::CK_VCallOffset:
770 return addOffsetConstant(CGM, builder, component.getVCallOffset());
771
772 case VTableComponent::CK_VBaseOffset:
773 return addOffsetConstant(CGM, builder, component.getVBaseOffset());
774
775 case VTableComponent::CK_OffsetToTop:
776 return addOffsetConstant(CGM, builder, component.getOffsetToTop());
777
778 case VTableComponent::CK_RTTI:
779 if (RelativeCXXABIVTables)
780 return addRelativeComponent(builder, component: rtti, vtableAddressPoint,
781 vtableHasLocalLinkage,
782 /*isCompleteDtor=*/false);
783 else
784 return builder.add(value: rtti);
785
786 case VTableComponent::CK_FunctionPointer:
787 case VTableComponent::CK_CompleteDtorPointer:
788 case VTableComponent::CK_DeletingDtorPointer: {
789 GlobalDecl GD = component.getGlobalDecl(
790 HasVectorDeletingDtors: CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
791 CGM.getContext().getLangOpts()));
792
793 const bool IsThunk =
794 nextVTableThunkIndex < layout.vtable_thunks().size() &&
795 layout.vtable_thunks()[nextVTableThunkIndex].first == componentIndex;
796
797 if (CGM.getLangOpts().CUDA) {
798 // Emit NULL for methods we can't codegen on this
799 // side. Otherwise we'd end up with vtable with unresolved
800 // references.
801 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
802 // OK on device side: functions w/ __device__ attribute
803 // OK on host side: anything except __device__-only functions.
804 bool CanEmitMethod =
805 CGM.getLangOpts().CUDAIsDevice
806 ? MD->hasAttr<CUDADeviceAttr>()
807 : (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>());
808 if (!CanEmitMethod) {
809 if (IsThunk)
810 nextVTableThunkIndex++;
811 return builder.add(
812 value: llvm::ConstantExpr::getNullValue(Ty: CGM.GlobalsInt8PtrTy));
813 }
814 // Method is acceptable, continue processing as usual.
815 }
816
817 auto getSpecialVirtualFn = [&](StringRef name) -> llvm::Constant * {
818 // FIXME(PR43094): When merging comdat groups, lld can select a local
819 // symbol as the signature symbol even though it cannot be accessed
820 // outside that symbol's TU. The relative vtables ABI would make
821 // __cxa_pure_virtual and __cxa_deleted_virtual local symbols, and
822 // depending on link order, the comdat groups could resolve to the one
823 // with the local symbol. As a temporary solution, fill these components
824 // with zero. We shouldn't be calling these in the first place anyway.
825 if (RelativeCXXABIVTables)
826 return llvm::ConstantPointerNull::get(T: CGM.GlobalsInt8PtrTy);
827
828 // For NVPTX devices in OpenMP emit special functon as null pointers,
829 // otherwise linking ends up with unresolved references.
830 if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsTargetDevice &&
831 CGM.getTriple().isNVPTX())
832 return llvm::ConstantPointerNull::get(T: CGM.GlobalsInt8PtrTy);
833 llvm::FunctionType *fnTy =
834 llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false);
835 llvm::Constant *fn = cast<llvm::Constant>(
836 Val: CGM.CreateRuntimeFunction(Ty: fnTy, Name: name).getCallee());
837 if (auto f = dyn_cast<llvm::Function>(Val: fn))
838 f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
839 return fn;
840 };
841
842 llvm::Constant *fnPtr;
843
844 // Pure virtual member functions.
845 if (cast<CXXMethodDecl>(Val: GD.getDecl())->isPureVirtual()) {
846 if (!PureVirtualFn)
847 PureVirtualFn =
848 getSpecialVirtualFn(CGM.getCXXABI().GetPureVirtualCallName());
849 fnPtr = PureVirtualFn;
850
851 // Deleted virtual member functions.
852 } else if (cast<CXXMethodDecl>(Val: GD.getDecl())->isDeleted()) {
853 if (!DeletedVirtualFn)
854 DeletedVirtualFn =
855 getSpecialVirtualFn(CGM.getCXXABI().GetDeletedVirtualCallName());
856 fnPtr = DeletedVirtualFn;
857
858 // Thunks.
859 } else if (IsThunk) {
860 auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second;
861
862 nextVTableThunkIndex++;
863 fnPtr = maybeEmitThunk(GD, TI: thunkInfo, /*ForVTable=*/true);
864 if (CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers) {
865 assert(thunkInfo.Method && "Method not set");
866 GD = GD.getWithDecl(D: thunkInfo.Method);
867 }
868
869 // Otherwise we can use the method definition directly.
870 } else {
871 llvm::Type *fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
872 fnPtr = CGM.GetAddrOfFunction(GD, Ty: fnTy, /*ForVTable=*/true);
873 if (CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers)
874 GD = getItaniumVTableContext().findOriginalMethod(GD);
875 }
876
877 if (RelativeCXXABIVTables) {
878 return addRelativeComponent(
879 builder, component: fnPtr, vtableAddressPoint, vtableHasLocalLinkage,
880 isCompleteDtor: component.getKind() == VTableComponent::CK_CompleteDtorPointer);
881 } else {
882 // TODO: this icky and only exists due to functions being in the generic
883 // address space, rather than the global one, even though they are
884 // globals; fixing said issue might be intrusive, and will be done
885 // later.
886 unsigned FnAS = fnPtr->getType()->getPointerAddressSpace();
887 unsigned GVAS = CGM.GlobalsInt8PtrTy->getPointerAddressSpace();
888
889 if (FnAS != GVAS)
890 fnPtr =
891 llvm::ConstantExpr::getAddrSpaceCast(C: fnPtr, Ty: CGM.GlobalsInt8PtrTy);
892 if (const auto &Schema =
893 CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers)
894 return builder.addSignedPointer(Pointer: fnPtr, Schema, CalleeDecl: GD, CalleeType: QualType());
895 return builder.add(value: fnPtr);
896 }
897 }
898
899 case VTableComponent::CK_UnusedFunctionPointer:
900 if (RelativeCXXABIVTables)
901 return builder.add(value: llvm::ConstantExpr::getNullValue(Ty: CGM.Int32Ty));
902 else
903 return builder.addNullPointer(ptrTy: CGM.GlobalsInt8PtrTy);
904 }
905
906 llvm_unreachable("Unexpected vtable component kind");
907}
908
909llvm::Type *CodeGenVTables::getVTableType(const VTableLayout &layout) {
910 SmallVector<llvm::Type *, 4> tys;
911 llvm::Type *componentType = getVTableComponentType();
912 for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i)
913 tys.push_back(Elt: llvm::ArrayType::get(ElementType: componentType, NumElements: layout.getVTableSize(i)));
914
915 return llvm::StructType::get(Context&: CGM.getLLVMContext(), Elements: tys);
916}
917
918void CodeGenVTables::createVTableInitializer(ConstantStructBuilder &builder,
919 const VTableLayout &layout,
920 llvm::Constant *rtti,
921 bool vtableHasLocalLinkage) {
922 llvm::Type *componentType = getVTableComponentType();
923
924 const auto &addressPoints = layout.getAddressPointIndices();
925 unsigned nextVTableThunkIndex = 0;
926 for (unsigned vtableIndex = 0, endIndex = layout.getNumVTables();
927 vtableIndex != endIndex; ++vtableIndex) {
928 auto vtableElem = builder.beginArray(eltTy: componentType);
929
930 size_t vtableStart = layout.getVTableOffset(i: vtableIndex);
931 size_t vtableEnd = vtableStart + layout.getVTableSize(i: vtableIndex);
932 for (size_t componentIndex = vtableStart; componentIndex < vtableEnd;
933 ++componentIndex) {
934 addVTableComponent(builder&: vtableElem, layout, componentIndex, rtti,
935 nextVTableThunkIndex, vtableAddressPoint: addressPoints[vtableIndex],
936 vtableHasLocalLinkage);
937 }
938 vtableElem.finishAndAddTo(parent&: builder);
939 }
940}
941
942llvm::GlobalVariable *CodeGenVTables::GenerateConstructionVTable(
943 const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual,
944 llvm::GlobalVariable::LinkageTypes Linkage,
945 VTableAddressPointsMapTy &AddressPoints) {
946 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
947 DI->completeClassData(RD: Base.getBase());
948
949 std::unique_ptr<VTableLayout> VTLayout(
950 getItaniumVTableContext().createConstructionVTableLayout(
951 MostDerivedClass: Base.getBase(), MostDerivedClassOffset: Base.getBaseOffset(), MostDerivedClassIsVirtual: BaseIsVirtual, LayoutClass: RD));
952
953 // Add the address points.
954 AddressPoints = VTLayout->getAddressPoints();
955
956 // Get the mangled construction vtable name.
957 SmallString<256> OutName;
958 llvm::raw_svector_ostream Out(OutName);
959 cast<ItaniumMangleContext>(Val&: CGM.getCXXABI().getMangleContext())
960 .mangleCXXCtorVTable(RD, Offset: Base.getBaseOffset().getQuantity(),
961 Type: Base.getBase(), Out);
962 SmallString<256> Name(OutName);
963
964 bool UsingRelativeLayout = CGM.getLangOpts().RelativeCXXABIVTables;
965 bool VTableAliasExists =
966 UsingRelativeLayout && CGM.getModule().getNamedAlias(Name);
967 if (VTableAliasExists) {
968 // We previously made the vtable hidden and changed its name.
969 Name.append(RHS: ".local");
970 }
971
972 llvm::Type *VTType = getVTableType(layout: *VTLayout);
973
974 // Construction vtable symbols are not part of the Itanium ABI, so we cannot
975 // guarantee that they actually will be available externally. Instead, when
976 // emitting an available_externally VTT, we provide references to an internal
977 // linkage construction vtable. The ABI only requires complete-object vtables
978 // to be the same for all instances of a type, not construction vtables.
979 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
980 Linkage = llvm::GlobalVariable::InternalLinkage;
981
982 llvm::Align Align = CGM.getDataLayout().getABITypeAlign(Ty: VTType);
983
984 // Create the variable that will hold the construction vtable.
985 llvm::GlobalVariable *VTable =
986 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Ty: VTType, Linkage, Alignment: Align);
987
988 // V-tables are always unnamed_addr.
989 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
990
991 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
992 Ty: CGM.getContext().getCanonicalTagType(TD: Base.getBase()));
993
994 // Create and set the initializer.
995 ConstantInitBuilder builder(CGM);
996 auto components = builder.beginStruct();
997 createVTableInitializer(builder&: components, layout: *VTLayout, rtti: RTTI,
998 vtableHasLocalLinkage: VTable->hasLocalLinkage());
999 components.finishAndSetAsInitializer(global: VTable);
1000
1001 // Set properties only after the initializer has been set to ensure that the
1002 // GV is treated as definition and not declaration.
1003 assert(!VTable->isDeclaration() && "Shouldn't set properties on declaration");
1004 CGM.setGVProperties(GV: VTable, D: RD);
1005
1006 CGM.EmitVTableTypeMetadata(RD, VTable, VTLayout: *VTLayout);
1007
1008 if (UsingRelativeLayout) {
1009 RemoveHwasanMetadata(GV: VTable);
1010 if (!VTable->isDSOLocal())
1011 GenerateRelativeVTableAlias(VTable, AliasNameRef: OutName);
1012 }
1013
1014 return VTable;
1015}
1016
1017// Ensure this vtable is not instrumented by hwasan. That is, a global alias is
1018// not generated for it. This is mainly used by the relative-vtables ABI where
1019// vtables instead contain 32-bit offsets between the vtable and function
1020// pointers. Hwasan is disabled for these vtables for now because the tag in a
1021// vtable pointer may fail the overflow check when resolving 32-bit PLT
1022// relocations. A future alternative for this would be finding which usages of
1023// the vtable can continue to use the untagged hwasan value without any loss of
1024// value in hwasan.
1025void CodeGenVTables::RemoveHwasanMetadata(llvm::GlobalValue *GV) const {
1026 if (CGM.getLangOpts().Sanitize.has(K: SanitizerKind::HWAddress)) {
1027 llvm::GlobalValue::SanitizerMetadata Meta;
1028 if (GV->hasSanitizerMetadata())
1029 Meta = GV->getSanitizerMetadata();
1030 Meta.NoHWAddress = true;
1031 GV->setSanitizerMetadata(Meta);
1032 }
1033}
1034
1035// If the VTable is not dso_local, then we will not be able to indicate that
1036// the VTable does not need a relocation and move into rodata. A frequent
1037// time this can occur is for classes that should be made public from a DSO
1038// (like in libc++). For cases like these, we can make the vtable hidden or
1039// internal and create a public alias with the same visibility and linkage as
1040// the original vtable type.
1041void CodeGenVTables::GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,
1042 llvm::StringRef AliasNameRef) {
1043 assert(CGM.getLangOpts().RelativeCXXABIVTables &&
1044 "Can only use this if the relative vtable ABI is used");
1045 assert(!VTable->isDSOLocal() && "This should be called only if the vtable is "
1046 "not guaranteed to be dso_local");
1047
1048 // If the vtable is available_externally, we shouldn't (or need to) generate
1049 // an alias for it in the first place since the vtable won't actually by
1050 // emitted in this compilation unit.
1051 if (VTable->hasAvailableExternallyLinkage())
1052 return;
1053
1054 // Create a new string in the event the alias is already the name of the
1055 // vtable. Using the reference directly could lead to use of an inititialized
1056 // value in the module's StringMap.
1057 llvm::SmallString<256> AliasName(AliasNameRef);
1058 VTable->setName(AliasName + ".local");
1059
1060 auto Linkage = VTable->getLinkage();
1061 assert(llvm::GlobalAlias::isValidLinkage(Linkage) &&
1062 "Invalid vtable alias linkage");
1063
1064 llvm::GlobalAlias *VTableAlias = CGM.getModule().getNamedAlias(Name: AliasName);
1065 if (!VTableAlias) {
1066 VTableAlias = llvm::GlobalAlias::create(Ty: VTable->getValueType(),
1067 AddressSpace: VTable->getAddressSpace(), Linkage,
1068 Name: AliasName, Parent: &CGM.getModule());
1069 } else {
1070 assert(VTableAlias->getValueType() == VTable->getValueType());
1071 assert(VTableAlias->getLinkage() == Linkage);
1072 }
1073 VTableAlias->setVisibility(VTable->getVisibility());
1074 VTableAlias->setUnnamedAddr(VTable->getUnnamedAddr());
1075
1076 // Both of these will now imply dso_local for the vtable.
1077 if (!VTable->hasComdat()) {
1078 VTable->setLinkage(llvm::GlobalValue::InternalLinkage);
1079 } else {
1080 // If a relocation targets an internal linkage symbol, MC will generate the
1081 // relocation against the symbol's section instead of the symbol itself
1082 // (see ELFObjectWriter::shouldRelocateWithSymbol). If an internal symbol is
1083 // in a COMDAT section group, that section might be discarded, and then the
1084 // relocation to that section will generate a linker error. We therefore
1085 // make COMDAT vtables hidden instead of internal: they'll still not be
1086 // public, but relocations will reference the symbol instead of the section
1087 // and COMDAT deduplication will thus work as expected.
1088 VTable->setVisibility(llvm::GlobalValue::HiddenVisibility);
1089 }
1090
1091 VTableAlias->setAliasee(VTable);
1092}
1093
1094static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM,
1095 const CXXRecordDecl *RD) {
1096 return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1097 CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
1098}
1099
1100/// Compute the required linkage of the vtable for the given class.
1101///
1102/// Note that we only call this at the end of the translation unit.
1103llvm::GlobalVariable::LinkageTypes
1104CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1105 if (!RD->isExternallyVisible())
1106 return llvm::GlobalVariable::InternalLinkage;
1107
1108 // In windows, the linkage of vtable is not related to modules.
1109 bool IsInNamedModule = !getTarget().getCXXABI().isMicrosoft() &&
1110 RD->isInNamedModule();
1111 // If the CXXRecordDecl is not in a module unit, we need to get
1112 // its key function. We're at the end of the translation unit, so the current
1113 // key function is fully correct.
1114 const CXXMethodDecl *keyFunction =
1115 IsInNamedModule ? nullptr : Context.getCurrentKeyFunction(RD);
1116 if (IsInNamedModule || (keyFunction && !RD->hasAttr<DLLImportAttr>())) {
1117 // If this class has a key function, use that to determine the
1118 // linkage of the vtable.
1119 const FunctionDecl *def = nullptr;
1120 if (keyFunction && keyFunction->hasBody(Definition&: def))
1121 keyFunction = cast<CXXMethodDecl>(Val: def);
1122
1123 bool IsExternalDefinition =
1124 IsInNamedModule ? RD->shouldEmitInExternalSource() : !def;
1125
1126 TemplateSpecializationKind Kind =
1127 IsInNamedModule ? RD->getTemplateSpecializationKind()
1128 : keyFunction->getTemplateSpecializationKind();
1129
1130 switch (Kind) {
1131 case TSK_Undeclared:
1132 case TSK_ExplicitSpecialization:
1133 assert(
1134 (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
1135 CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo) &&
1136 "Shouldn't query vtable linkage without the class in module units, "
1137 "key function, optimizations, or debug info");
1138 if (IsExternalDefinition && CodeGenOpts.OptimizationLevel > 0)
1139 return llvm::GlobalVariable::AvailableExternallyLinkage;
1140
1141 if (keyFunction && keyFunction->isInlined())
1142 return !Context.getLangOpts().AppleKext
1143 ? llvm::GlobalVariable::LinkOnceODRLinkage
1144 : llvm::Function::InternalLinkage;
1145
1146 return llvm::GlobalVariable::ExternalLinkage;
1147
1148 case TSK_ImplicitInstantiation:
1149 return !Context.getLangOpts().AppleKext ?
1150 llvm::GlobalVariable::LinkOnceODRLinkage :
1151 llvm::Function::InternalLinkage;
1152
1153 case TSK_ExplicitInstantiationDefinition:
1154 return !Context.getLangOpts().AppleKext ?
1155 llvm::GlobalVariable::WeakODRLinkage :
1156 llvm::Function::InternalLinkage;
1157
1158 case TSK_ExplicitInstantiationDeclaration:
1159 return IsExternalDefinition
1160 ? llvm::GlobalVariable::AvailableExternallyLinkage
1161 : llvm::GlobalVariable::ExternalLinkage;
1162 }
1163 }
1164
1165 // -fapple-kext mode does not support weak linkage, so we must use
1166 // internal linkage.
1167 if (Context.getLangOpts().AppleKext)
1168 return llvm::Function::InternalLinkage;
1169
1170 llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
1171 llvm::GlobalValue::LinkOnceODRLinkage;
1172 llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
1173 llvm::GlobalValue::WeakODRLinkage;
1174 if (RD->hasAttr<DLLExportAttr>()) {
1175 // Cannot discard exported vtables.
1176 DiscardableODRLinkage = NonDiscardableODRLinkage;
1177 } else if (RD->hasAttr<DLLImportAttr>()) {
1178 // Imported vtables are available externally.
1179 DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
1180 NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
1181 }
1182
1183 switch (RD->getTemplateSpecializationKind()) {
1184 case TSK_Undeclared:
1185 case TSK_ExplicitSpecialization:
1186 case TSK_ImplicitInstantiation:
1187 return DiscardableODRLinkage;
1188
1189 case TSK_ExplicitInstantiationDeclaration:
1190 // Explicit instantiations in MSVC do not provide vtables, so we must emit
1191 // our own.
1192 if (getTarget().getCXXABI().isMicrosoft())
1193 return DiscardableODRLinkage;
1194 return shouldEmitAvailableExternallyVTable(CGM: *this, RD)
1195 ? llvm::GlobalVariable::AvailableExternallyLinkage
1196 : llvm::GlobalVariable::ExternalLinkage;
1197
1198 case TSK_ExplicitInstantiationDefinition:
1199 return NonDiscardableODRLinkage;
1200 }
1201
1202 llvm_unreachable("Invalid TemplateSpecializationKind!");
1203}
1204
1205/// This is a callback from Sema to tell us that a particular vtable is
1206/// required to be emitted in this translation unit.
1207///
1208/// This is only called for vtables that _must_ be emitted (mainly due to key
1209/// functions). For weak vtables, CodeGen tracks when they are needed and
1210/// emits them as-needed.
1211void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
1212 VTables.GenerateClassData(RD: theClass);
1213}
1214
1215void
1216CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
1217 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
1218 DI->completeClassData(RD);
1219
1220 if (RD->getNumVBases())
1221 CGM.getCXXABI().emitVirtualInheritanceTables(RD);
1222
1223 CGM.getCXXABI().emitVTableDefinitions(CGVT&: *this, RD);
1224}
1225
1226/// At this point in the translation unit, does it appear that can we
1227/// rely on the vtable being defined elsewhere in the program?
1228///
1229/// The response is really only definitive when called at the end of
1230/// the translation unit.
1231///
1232/// The only semantic restriction here is that the object file should
1233/// not contain a vtable definition when that vtable is defined
1234/// strongly elsewhere. Otherwise, we'd just like to avoid emitting
1235/// vtables when unnecessary.
1236bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
1237 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
1238
1239 // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
1240 // emit them even if there is an explicit template instantiation.
1241 if (CGM.getTarget().getCXXABI().isMicrosoft())
1242 return false;
1243
1244 // If we have an explicit instantiation declaration (and not a
1245 // definition), the vtable is defined elsewhere.
1246 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1247 if (TSK == TSK_ExplicitInstantiationDeclaration)
1248 return true;
1249
1250 // Otherwise, if the class is an instantiated template, the
1251 // vtable must be defined here.
1252 if (TSK == TSK_ImplicitInstantiation ||
1253 TSK == TSK_ExplicitInstantiationDefinition)
1254 return false;
1255
1256 // Otherwise, if the class is attached to a module, the tables are uniquely
1257 // emitted in the object for the module unit in which it is defined.
1258 if (RD->isInNamedModule())
1259 return RD->shouldEmitInExternalSource();
1260
1261 // Otherwise, if the class doesn't have a key function (possibly
1262 // anymore), the vtable must be defined here.
1263 const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
1264 if (!keyFunction)
1265 return false;
1266
1267 // Otherwise, if we don't have a definition of the key function, the
1268 // vtable must be defined somewhere else.
1269 return !keyFunction->hasBody();
1270}
1271
1272/// Given that we're currently at the end of the translation unit, and
1273/// we've emitted a reference to the vtable for this class, should
1274/// we define that vtable?
1275static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
1276 const CXXRecordDecl *RD) {
1277 // If vtable is internal then it has to be done.
1278 if (!CGM.getVTables().isVTableExternal(RD))
1279 return true;
1280
1281 // If it's external then maybe we will need it as available_externally.
1282 return shouldEmitAvailableExternallyVTable(CGM, RD);
1283}
1284
1285/// Given that at some point we emitted a reference to one or more
1286/// vtables, and that we are now at the end of the translation unit,
1287/// decide whether we should emit them.
1288void CodeGenModule::EmitDeferredVTables() {
1289#ifndef NDEBUG
1290 // Remember the size of DeferredVTables, because we're going to assume
1291 // that this entire operation doesn't modify it.
1292 size_t savedSize = DeferredVTables.size();
1293#endif
1294
1295 for (const CXXRecordDecl *RD : DeferredVTables)
1296 if (shouldEmitVTableAtEndOfTranslationUnit(CGM&: *this, RD))
1297 VTables.GenerateClassData(RD);
1298 else if (shouldOpportunisticallyEmitVTables())
1299 OpportunisticVTables.push_back(x: RD);
1300
1301 assert(savedSize == DeferredVTables.size() &&
1302 "deferred extra vtables during vtable emission?");
1303 DeferredVTables.clear();
1304}
1305
1306bool CodeGenModule::AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD) {
1307 if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>() ||
1308 RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
1309 return true;
1310
1311 if (!getCodeGenOpts().LTOVisibilityPublicStd)
1312 return false;
1313
1314 const DeclContext *DC = RD;
1315 while (true) {
1316 auto *D = cast<Decl>(Val: DC);
1317 DC = DC->getParent();
1318 if (isa<TranslationUnitDecl>(Val: DC->getRedeclContext())) {
1319 if (auto *ND = dyn_cast<NamespaceDecl>(Val: D))
1320 if (const IdentifierInfo *II = ND->getIdentifier())
1321 if (II->isStr(Str: "std") || II->isStr(Str: "stdext"))
1322 return true;
1323 break;
1324 }
1325 }
1326
1327 return false;
1328}
1329
1330bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) {
1331 LinkageInfo LV = RD->getLinkageAndVisibility();
1332 if (!isExternallyVisible(L: LV.getLinkage()))
1333 return true;
1334
1335 if (!getTriple().isOSBinFormatCOFF() &&
1336 LV.getVisibility() != HiddenVisibility)
1337 return false;
1338
1339 return !AlwaysHasLTOVisibilityPublic(RD);
1340}
1341
1342llvm::GlobalObject::VCallVisibility CodeGenModule::GetVCallVisibilityLevel(
1343 const CXXRecordDecl *RD, llvm::DenseSet<const CXXRecordDecl *> &Visited) {
1344 // If we have already visited this RD (which means this is a recursive call
1345 // since the initial call should have an empty Visited set), return the max
1346 // visibility. The recursive calls below compute the min between the result
1347 // of the recursive call and the current TypeVis, so returning the max here
1348 // ensures that it will have no effect on the current TypeVis.
1349 if (!Visited.insert(V: RD).second)
1350 return llvm::GlobalObject::VCallVisibilityTranslationUnit;
1351
1352 LinkageInfo LV = RD->getLinkageAndVisibility();
1353 llvm::GlobalObject::VCallVisibility TypeVis;
1354 if (!isExternallyVisible(L: LV.getLinkage()))
1355 TypeVis = llvm::GlobalObject::VCallVisibilityTranslationUnit;
1356 else if (HasHiddenLTOVisibility(RD))
1357 TypeVis = llvm::GlobalObject::VCallVisibilityLinkageUnit;
1358 else
1359 TypeVis = llvm::GlobalObject::VCallVisibilityPublic;
1360
1361 for (const auto &B : RD->bases())
1362 if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
1363 TypeVis = std::min(
1364 a: TypeVis,
1365 b: GetVCallVisibilityLevel(RD: B.getType()->getAsCXXRecordDecl(), Visited));
1366
1367 for (const auto &B : RD->vbases())
1368 if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
1369 TypeVis = std::min(
1370 a: TypeVis,
1371 b: GetVCallVisibilityLevel(RD: B.getType()->getAsCXXRecordDecl(), Visited));
1372
1373 return TypeVis;
1374}
1375
1376void CodeGenModule::EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1377 llvm::GlobalVariable *VTable,
1378 const VTableLayout &VTLayout) {
1379 // Emit type metadata on vtables with LTO or IR instrumentation or
1380 // speculative devirtualization.
1381 // In IR instrumentation, the type metadata is used to find out vtable
1382 // definitions (for type profiling) among all global variables.
1383 if (!getCodeGenOpts().LTOUnit && !getCodeGenOpts().hasProfileIRInstr() &&
1384 !getCodeGenOpts().DevirtualizeSpeculatively)
1385 return;
1386
1387 CharUnits ComponentWidth = GetTargetTypeStoreSize(Ty: getVTableComponentType());
1388
1389 struct AddressPoint {
1390 const CXXRecordDecl *Base;
1391 size_t Offset;
1392 std::string TypeName;
1393 bool operator<(const AddressPoint &RHS) const {
1394 int D = TypeName.compare(str: RHS.TypeName);
1395 return D < 0 || (D == 0 && Offset < RHS.Offset);
1396 }
1397 };
1398 std::vector<AddressPoint> AddressPoints;
1399 for (auto &&AP : VTLayout.getAddressPoints()) {
1400 AddressPoint N{.Base: AP.first.getBase(),
1401 .Offset: VTLayout.getVTableOffset(i: AP.second.VTableIndex) +
1402 AP.second.AddressPointIndex,
1403 .TypeName: {}};
1404 llvm::raw_string_ostream Stream(N.TypeName);
1405 CanQualType T = getContext().getCanonicalTagType(TD: N.Base);
1406 getCXXABI().getMangleContext().mangleCanonicalTypeName(T, Stream);
1407 AddressPoints.push_back(x: std::move(N));
1408 }
1409
1410 // Sort the address points for determinism.
1411 llvm::sort(C&: AddressPoints);
1412
1413 ArrayRef<VTableComponent> Comps = VTLayout.vtable_components();
1414 for (auto AP : AddressPoints) {
1415 // Create type metadata for the address point.
1416 AddVTableTypeMetadata(VTable, Offset: ComponentWidth * AP.Offset, RD: AP.Base);
1417
1418 // The class associated with each address point could also potentially be
1419 // used for indirect calls via a member function pointer, so we need to
1420 // annotate the address of each function pointer with the appropriate member
1421 // function pointer type.
1422 for (unsigned I = 0; I != Comps.size(); ++I) {
1423 if (Comps[I].getKind() != VTableComponent::CK_FunctionPointer)
1424 continue;
1425 llvm::Metadata *MD = CreateMetadataIdentifierForVirtualMemPtrType(
1426 T: Context.getMemberPointerType(T: Comps[I].getFunctionDecl()->getType(),
1427 /*Qualifier=*/std::nullopt, Cls: AP.Base));
1428 VTable->addTypeMetadata(Offset: (ComponentWidth * I).getQuantity(), TypeID: MD);
1429 }
1430 }
1431
1432 if (getCodeGenOpts().VirtualFunctionElimination ||
1433 getCodeGenOpts().WholeProgramVTables) {
1434 llvm::DenseSet<const CXXRecordDecl *> Visited;
1435 llvm::GlobalObject::VCallVisibility TypeVis =
1436 GetVCallVisibilityLevel(RD, Visited);
1437 if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
1438 VTable->setVCallVisibilityMetadata(TypeVis);
1439 }
1440}
1441