Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 1 | //===- UDTLayout.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DebugInfo/PDB/UDTLayout.h" |
| 11 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/IPDBSession.h" |
| 14 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
| 15 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
| 16 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 17 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 18 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h" |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h" |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 22 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h" |
| 23 | |
| 24 | #include <utility> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace llvm::pdb; |
| 28 | |
| 29 | static std::unique_ptr<PDBSymbol> getSymbolType(const PDBSymbol &Symbol) { |
| 30 | const IPDBSession &Session = Symbol.getSession(); |
| 31 | const IPDBRawSymbol &RawSymbol = Symbol.getRawSymbol(); |
| 32 | uint32_t TypeId = RawSymbol.getTypeId(); |
| 33 | return Session.getSymbolById(TypeId); |
| 34 | } |
| 35 | |
| 36 | static uint32_t getTypeLength(const PDBSymbol &Symbol) { |
| 37 | auto SymbolType = getSymbolType(Symbol); |
| 38 | const IPDBRawSymbol &RawType = SymbolType->getRawSymbol(); |
| 39 | |
| 40 | return RawType.getLength(); |
| 41 | } |
| 42 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 43 | LayoutItemBase::LayoutItemBase(const UDTLayoutBase *Parent, |
| 44 | const PDBSymbol *Symbol, const std::string &Name, |
| 45 | uint32_t OffsetInParent, uint32_t Size, |
| 46 | bool IsElided) |
| 47 | : Symbol(Symbol), Parent(Parent), Name(Name), |
| 48 | OffsetInParent(OffsetInParent), SizeOf(Size), LayoutSize(Size), |
| 49 | IsElided(IsElided) { |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 50 | UsedBytes.resize(SizeOf, true); |
| 51 | } |
| 52 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 53 | uint32_t LayoutItemBase::deepPaddingSize() const { |
| 54 | return UsedBytes.size() - UsedBytes.count(); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 57 | uint32_t LayoutItemBase::tailPadding() const { |
| 58 | int Last = UsedBytes.find_last(); |
| 59 | |
| 60 | return UsedBytes.size() - (Last + 1); |
| 61 | } |
| 62 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 63 | DataMemberLayoutItem::DataMemberLayoutItem( |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 64 | const UDTLayoutBase &Parent, std::unique_ptr<PDBSymbolData> Member) |
| 65 | : LayoutItemBase(&Parent, Member.get(), Member->getName(), |
| 66 | Member->getOffset(), getTypeLength(*Member), false), |
| 67 | DataMember(std::move(Member)) { |
| 68 | auto Type = DataMember->getType(); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 69 | if (auto UDT = unique_dyn_cast<PDBSymbolTypeUDT>(Type)) { |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 70 | UdtLayout = llvm::make_unique<ClassLayout>(std::move(UDT)); |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 71 | UsedBytes = UdtLayout->usedBytes(); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 75 | VBPtrLayoutItem::VBPtrLayoutItem(const UDTLayoutBase &Parent, |
| 76 | std::unique_ptr<PDBSymbolTypeBuiltin> Sym, |
| 77 | uint32_t Offset, uint32_t Size) |
| 78 | : LayoutItemBase(&Parent, Sym.get(), "<vbptr>", Offset, Size, false), |
| 79 | Type(std::move(Sym)) { |
| 80 | } |
| 81 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 82 | const PDBSymbolData &DataMemberLayoutItem::getDataMember() { |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 83 | return *dyn_cast<PDBSymbolData>(Symbol); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 86 | bool DataMemberLayoutItem::hasUDTLayout() const { return UdtLayout != nullptr; } |
| 87 | |
| 88 | const ClassLayout &DataMemberLayoutItem::getUDTLayout() const { |
| 89 | return *UdtLayout; |
| 90 | } |
| 91 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 92 | VTableLayoutItem::VTableLayoutItem(const UDTLayoutBase &Parent, |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 93 | std::unique_ptr<PDBSymbolTypeVTable> VT) |
| 94 | : LayoutItemBase(&Parent, VT.get(), "<vtbl>", 0, getTypeLength(*VT), false), |
| 95 | VTable(std::move(VT)) { |
| 96 | auto VTableType = cast<PDBSymbolTypePointer>(VTable->getType()); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 97 | ElementSize = VTableType->getLength(); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 100 | UDTLayoutBase::UDTLayoutBase(const UDTLayoutBase *Parent, const PDBSymbol &Sym, |
| 101 | const std::string &Name, uint32_t OffsetInParent, |
| 102 | uint32_t Size, bool IsElided) |
| 103 | : LayoutItemBase(Parent, &Sym, Name, OffsetInParent, Size, IsElided) { |
| 104 | // UDT storage comes from a union of all the children's storage, so start out |
| 105 | // uninitialized. |
| 106 | UsedBytes.reset(0, Size); |
| 107 | |
| 108 | initializeChildren(Sym); |
| 109 | if (LayoutSize < Size) |
| 110 | UsedBytes.resize(LayoutSize); |
| 111 | } |
| 112 | |
| 113 | uint32_t UDTLayoutBase::tailPadding() const { |
| 114 | uint32_t Abs = LayoutItemBase::tailPadding(); |
| 115 | if (!LayoutItems.empty()) { |
| 116 | const LayoutItemBase *Back = LayoutItems.back(); |
| 117 | uint32_t ChildPadding = Back->LayoutItemBase::tailPadding(); |
| 118 | if (Abs < ChildPadding) |
| 119 | Abs = 0; |
| 120 | else |
| 121 | Abs -= ChildPadding; |
| 122 | } |
| 123 | return Abs; |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 126 | ClassLayout::ClassLayout(const PDBSymbolTypeUDT &UDT) |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 127 | : UDTLayoutBase(nullptr, UDT, UDT.getName(), 0, UDT.getLength(), false), |
Zachary Turner | da307b6 | 2017-04-25 20:22:29 +0000 | [diff] [blame^] | 128 | UDT(UDT) { |
| 129 | ImmediateUsedBytes.resize(SizeOf, false); |
| 130 | for (auto &LI : LayoutItems) { |
| 131 | uint32_t Begin = LI->getOffsetInParent(); |
| 132 | uint32_t End = Begin + LI->getLayoutSize(); |
| 133 | End = std::min(SizeOf, End); |
| 134 | ImmediateUsedBytes.set(Begin, End); |
| 135 | } |
| 136 | } |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 137 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 138 | ClassLayout::ClassLayout(std::unique_ptr<PDBSymbolTypeUDT> UDT) |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 139 | : ClassLayout(*UDT) { |
| 140 | OwnedStorage = std::move(UDT); |
| 141 | } |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 142 | |
Zachary Turner | da307b6 | 2017-04-25 20:22:29 +0000 | [diff] [blame^] | 143 | uint32_t ClassLayout::immediatePadding() const { |
| 144 | return SizeOf - ImmediateUsedBytes.count(); |
| 145 | } |
| 146 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 147 | BaseClassLayout::BaseClassLayout(const UDTLayoutBase &Parent, |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 148 | uint32_t OffsetInParent, bool Elide, |
| 149 | std::unique_ptr<PDBSymbolTypeBaseClass> B) |
| 150 | : UDTLayoutBase(&Parent, *B, B->getName(), OffsetInParent, B->getLength(), |
| 151 | Elide), |
| 152 | Base(std::move(B)) { |
| 153 | if (isEmptyBase()) { |
| 154 | // Special case an empty base so that it doesn't get treated as padding. |
| 155 | UsedBytes.resize(1); |
| 156 | UsedBytes.set(0); |
| 157 | } |
| 158 | IsVirtualBase = Base->isVirtualBaseClass(); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void UDTLayoutBase::initializeChildren(const PDBSymbol &Sym) { |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 162 | // Handled bases first, followed by VTables, followed by data members, |
| 163 | // followed by functions, followed by other. This ordering is necessary |
| 164 | // so that bases and vtables get initialized before any functions which |
| 165 | // may override them. |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 166 | UniquePtrVector<PDBSymbolTypeBaseClass> Bases; |
| 167 | UniquePtrVector<PDBSymbolTypeVTable> VTables; |
| 168 | UniquePtrVector<PDBSymbolData> Members; |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 169 | UniquePtrVector<PDBSymbolTypeBaseClass> VirtualBaseSyms; |
| 170 | |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 171 | auto Children = Sym.findAllChildren(); |
| 172 | while (auto Child = Children->getNext()) { |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 173 | if (auto Base = unique_dyn_cast<PDBSymbolTypeBaseClass>(Child)) { |
| 174 | if (Base->isVirtualBaseClass()) |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 175 | VirtualBaseSyms.push_back(std::move(Base)); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 176 | else |
| 177 | Bases.push_back(std::move(Base)); |
| 178 | } |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 179 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 180 | else if (auto Data = unique_dyn_cast<PDBSymbolData>(Child)) { |
| 181 | if (Data->getDataKind() == PDB_DataKind::Member) |
| 182 | Members.push_back(std::move(Data)); |
| 183 | else |
| 184 | Other.push_back(std::move(Child)); |
| 185 | } else if (auto VT = unique_dyn_cast<PDBSymbolTypeVTable>(Child)) |
| 186 | VTables.push_back(std::move(VT)); |
| 187 | else if (auto Func = unique_dyn_cast<PDBSymbolFunc>(Child)) |
| 188 | Funcs.push_back(std::move(Func)); |
| 189 | else |
| 190 | Other.push_back(std::move(Child)); |
| 191 | } |
| 192 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 193 | // We don't want to have any re-allocations in the list of bases, so make |
| 194 | // sure to reserve enough space so that our ArrayRefs don't get invalidated. |
| 195 | AllBases.reserve(Bases.size() + VirtualBaseSyms.size()); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 196 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 197 | // Only add non-virtual bases to the class first. Only at the end of the |
| 198 | // class, after all non-virtual bases and data members have been added do we |
| 199 | // add virtual bases. This way the offsets are correctly aligned when we go |
| 200 | // to lay out virtual bases. |
| 201 | for (auto &Base : Bases) { |
| 202 | uint32_t Offset = Base->getOffset(); |
| 203 | // Non-virtual bases never get elided. |
| 204 | auto BL = llvm::make_unique<BaseClassLayout>(*this, Offset, false, |
| 205 | std::move(Base)); |
| 206 | |
| 207 | AllBases.push_back(BL.get()); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 208 | addChildToLayout(std::move(BL)); |
| 209 | } |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 210 | NonVirtualBases = AllBases; |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 211 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 212 | assert(VTables.size() <= 1); |
| 213 | if (!VTables.empty()) { |
| 214 | auto VTLayout = |
| 215 | llvm::make_unique<VTableLayoutItem>(*this, std::move(VTables[0])); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 216 | |
| 217 | VTable = VTLayout.get(); |
| 218 | |
| 219 | addChildToLayout(std::move(VTLayout)); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | for (auto &Data : Members) { |
| 223 | auto DM = llvm::make_unique<DataMemberLayoutItem>(*this, std::move(Data)); |
| 224 | |
| 225 | addChildToLayout(std::move(DM)); |
| 226 | } |
| 227 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 228 | // Make sure add virtual bases before adding functions, since functions may be |
| 229 | // overrides of virtual functions declared in a virtual base, so the VTables |
| 230 | // and virtual intros need to be correctly initialized. |
| 231 | for (auto &VB : VirtualBaseSyms) { |
| 232 | int VBPO = VB->getVirtualBasePointerOffset(); |
| 233 | if (!hasVBPtrAtOffset(VBPO)) { |
| 234 | if (auto VBP = VB->getRawSymbol().getVirtualBaseTableType()) { |
| 235 | auto VBPL = llvm::make_unique<VBPtrLayoutItem>(*this, std::move(VBP), |
| 236 | VBPO, VBP->getLength()); |
| 237 | VBPtr = VBPL.get(); |
| 238 | addChildToLayout(std::move(VBPL)); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 241 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 242 | // Virtual bases always go at the end. So just look for the last place we |
| 243 | // ended when writing something, and put our virtual base there. |
| 244 | // Note that virtual bases get elided unless this is a top-most derived |
| 245 | // class. |
| 246 | uint32_t Offset = UsedBytes.find_last() + 1; |
| 247 | bool Elide = (Parent != nullptr); |
| 248 | auto BL = |
| 249 | llvm::make_unique<BaseClassLayout>(*this, Offset, Elide, std::move(VB)); |
| 250 | AllBases.push_back(BL.get()); |
| 251 | |
| 252 | // Only lay this virtual base out directly inside of *this* class if this |
| 253 | // is a top-most derived class. Keep track of it regardless, but only |
| 254 | // physically lay it out if it's a topmost derived class. |
| 255 | addChildToLayout(std::move(BL)); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 256 | } |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 257 | VirtualBases = makeArrayRef(AllBases).drop_front(NonVirtualBases.size()); |
| 258 | |
| 259 | if (Parent != nullptr) |
| 260 | LayoutSize = UsedBytes.find_last() + 1; |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 263 | bool UDTLayoutBase::hasVBPtrAtOffset(uint32_t Off) const { |
| 264 | if (VBPtr && VBPtr->getOffsetInParent() == Off) |
| 265 | return true; |
| 266 | for (BaseClassLayout *BL : AllBases) { |
| 267 | if (BL->hasVBPtrAtOffset(Off - BL->getOffsetInParent())) |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | void UDTLayoutBase::addChildToLayout(std::unique_ptr<LayoutItemBase> Child) { |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 274 | uint32_t Begin = Child->getOffsetInParent(); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 275 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 276 | if (!Child->isElided()) { |
| 277 | BitVector ChildBytes = Child->usedBytes(); |
| 278 | |
| 279 | // Suppose the child occupies 4 bytes starting at offset 12 in a 32 byte |
| 280 | // class. When we call ChildBytes.resize(32), the Child's storage will |
| 281 | // still begin at offset 0, so we need to shift it left by offset bytes |
| 282 | // to get it into the right position. |
| 283 | ChildBytes.resize(UsedBytes.size()); |
| 284 | ChildBytes <<= Child->getOffsetInParent(); |
| 285 | UsedBytes |= ChildBytes; |
| 286 | |
| 287 | if (ChildBytes.count() > 0) { |
| 288 | auto Loc = std::upper_bound(LayoutItems.begin(), LayoutItems.end(), Begin, |
| 289 | [](uint32_t Off, const LayoutItemBase *Item) { |
| 290 | return (Off < Item->getOffsetInParent()); |
| 291 | }); |
| 292 | |
| 293 | LayoutItems.insert(Loc, Child.get()); |
| 294 | } |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 297 | ChildStorage.push_back(std::move(Child)); |
Zachary Turner | c883a8c | 2017-04-12 23:18:21 +0000 | [diff] [blame] | 298 | } |