161 lines · c
1//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This contains code dealing with C++ code generation of virtual tables.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H14#define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H15 16#include "clang/AST/BaseSubobject.h"17#include "clang/AST/CharUnits.h"18#include "clang/AST/GlobalDecl.h"19#include "clang/AST/VTableBuilder.h"20#include "clang/Basic/ABI.h"21#include "llvm/ADT/DenseMap.h"22#include "llvm/IR/GlobalVariable.h"23 24namespace clang {25 class CXXRecordDecl;26 27namespace CodeGen {28 class CodeGenModule;29 class ConstantArrayBuilder;30 class ConstantStructBuilder;31 32class CodeGenVTables {33 CodeGenModule &CGM;34 35 VTableContextBase *VTContext;36 37 /// VTableAddressPointsMapTy - Address points for a single vtable.38 typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;39 40 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;41 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndicesMapTy;42 43 /// SubVTTIndices - Contains indices into the various sub-VTTs.44 SubVTTIndicesMapTy SubVTTIndices;45 46 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>47 SecondaryVirtualPointerIndicesMapTy;48 49 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer50 /// indices.51 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;52 53 /// Cache for the pure virtual member call function.54 llvm::Constant *PureVirtualFn = nullptr;55 56 /// Cache for the deleted virtual member call function.57 llvm::Constant *DeletedVirtualFn = nullptr;58 59 /// Get the address of a thunk and emit it if necessary.60 llvm::Constant *maybeEmitThunk(GlobalDecl GD,61 const ThunkInfo &ThunkAdjustments,62 bool ForVTable);63 64 void addVTableComponent(ConstantArrayBuilder &builder,65 const VTableLayout &layout, unsigned componentIndex,66 llvm::Constant *rtti, unsigned &nextVTableThunkIndex,67 unsigned vtableAddressPoint,68 bool vtableHasLocalLinkage);69 70 /// Add a 32-bit offset to a component relative to the vtable when using the71 /// relative vtables ABI. The array builder points to the start of the vtable.72 void addRelativeComponent(ConstantArrayBuilder &builder,73 llvm::Constant *component,74 unsigned vtableAddressPoint,75 bool vtableHasLocalLinkage,76 bool isCompleteDtor) const;77 78public:79 /// Add vtable components for the given vtable layout to the given80 /// global initializer.81 void createVTableInitializer(ConstantStructBuilder &builder,82 const VTableLayout &layout, llvm::Constant *rtti,83 bool vtableHasLocalLinkage);84 85 CodeGenVTables(CodeGenModule &CGM);86 87 ItaniumVTableContext &getItaniumVTableContext() {88 return *cast<ItaniumVTableContext>(VTContext);89 }90 91 const ItaniumVTableContext &getItaniumVTableContext() const {92 return *cast<ItaniumVTableContext>(VTContext);93 }94 95 MicrosoftVTableContext &getMicrosoftVTableContext() {96 return *cast<MicrosoftVTableContext>(VTContext);97 }98 99 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the100 /// given record decl.101 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);102 103 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the104 /// virtual pointer for the given subobject is located.105 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,106 BaseSubobject Base);107 108 /// GenerateConstructionVTable - Generate a construction vtable for the given109 /// base subobject.110 llvm::GlobalVariable *111 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,112 bool BaseIsVirtual,113 llvm::GlobalVariable::LinkageTypes Linkage,114 VTableAddressPointsMapTy& AddressPoints);115 116 117 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.118 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);119 120 /// EmitVTTDefinition - Emit the definition of the given vtable.121 void EmitVTTDefinition(llvm::GlobalVariable *VTT,122 llvm::GlobalVariable::LinkageTypes Linkage,123 const CXXRecordDecl *RD);124 125 /// EmitThunks - Emit the associated thunks for the given global decl.126 void EmitThunks(GlobalDecl GD);127 128 /// GenerateClassData - Generate all the class data required to be129 /// generated upon definition of a KeyFunction. This includes the130 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT131 /// (if the class has virtual bases).132 void GenerateClassData(const CXXRecordDecl *RD);133 134 bool isVTableExternal(const CXXRecordDecl *RD);135 136 /// Returns the type of a vtable with the given layout. Normally a struct of137 /// arrays of pointers, with one struct element for each vtable in the vtable138 /// group.139 llvm::Type *getVTableType(const VTableLayout &layout);140 141 /// Generate a public facing alias for the vtable and make the vtable either142 /// hidden or private. The alias will have the original linkage and visibility143 /// of the vtable. This is used for cases under the relative vtables ABI144 /// when a vtable may not be dso_local.145 void GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,146 llvm::StringRef AliasNameRef);147 148 /// Specify a global should not be instrumented with hwasan.149 void RemoveHwasanMetadata(llvm::GlobalValue *GV) const;150 151 /// Return the type used as components for a vtable.152 llvm::Type *getVTableComponentType() const;153 154 /// Return true if the relative vtable layout is used.155 bool useRelativeLayout() const;156};157 158} // end namespace CodeGen159} // end namespace clang160#endif161