1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
//===- UDTLayout.cpp --------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/PDB/UDTLayout.h"
#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/PDBSymbol.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
#include <utility>
using namespace llvm;
using namespace llvm::pdb;
static std::unique_ptr<PDBSymbol> getSymbolType(const PDBSymbol &Symbol) {
const IPDBSession &Session = Symbol.getSession();
const IPDBRawSymbol &RawSymbol = Symbol.getRawSymbol();
uint32_t TypeId = RawSymbol.getTypeId();
return Session.getSymbolById(TypeId);
}
static uint32_t getTypeLength(const PDBSymbol &Symbol) {
auto SymbolType = getSymbolType(Symbol);
const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
return RawType.getLength();
}
StorageItemBase::StorageItemBase(const UDTLayoutBase &Parent,
const PDBSymbol &Symbol,
const std::string &Name,
uint32_t OffsetInParent, uint32_t Size)
: Parent(Parent), Symbol(Symbol), Name(Name),
OffsetInParent(OffsetInParent), SizeOf(Size) {
UsedBytes.resize(SizeOf, true);
}
uint32_t StorageItemBase::deepPaddingSize() const {
// sizeof(Field) - sizeof(typeof(Field)) is trailing padding.
return SizeOf - getTypeLength(Symbol);
}
DataMemberLayoutItem::DataMemberLayoutItem(
const UDTLayoutBase &Parent, std::unique_ptr<PDBSymbolData> DataMember)
: StorageItemBase(Parent, *DataMember, DataMember->getName(),
DataMember->getOffset(), getTypeLength(*DataMember)),
DataMember(std::move(DataMember)) {
auto Type = this->DataMember->getType();
if (auto UDT = unique_dyn_cast<PDBSymbolTypeUDT>(Type)) {
// UDT data members might have padding in between fields, but otherwise
// a member should occupy its entire storage.
UsedBytes.resize(SizeOf, false);
UdtLayout = llvm::make_unique<ClassLayout>(std::move(UDT));
}
}
const PDBSymbolData &DataMemberLayoutItem::getDataMember() {
return *dyn_cast<PDBSymbolData>(&Symbol);
}
uint32_t DataMemberLayoutItem::deepPaddingSize() const {
uint32_t Result = StorageItemBase::deepPaddingSize();
if (UdtLayout)
Result += UdtLayout->deepPaddingSize();
return Result;
}
VTableLayoutItem::VTableLayoutItem(const UDTLayoutBase &Parent,
std::unique_ptr<PDBSymbolTypeVTable> VTable)
: StorageItemBase(Parent, *VTable, "<vtbl>", 0, getTypeLength(*VTable)),
VTable(std::move(VTable)) {
// initialize vtbl methods.
auto VTableType = cast<PDBSymbolTypePointer>(this->VTable->getType());
uint32_t PointerSize = VTableType->getLength();
if (auto Shape = unique_dyn_cast<PDBSymbolTypeVTableShape>(
VTableType->getPointeeType())) {
VTableFuncs.resize(Shape->getCount());
auto ParentFunctions =
Parent.getSymbolBase().findAllChildren<PDBSymbolFunc>();
while (auto Func = ParentFunctions->getNext()) {
if (Func->isVirtual()) {
uint32_t Index = Func->getVirtualBaseOffset();
assert(Index % PointerSize == 0);
Index /= PointerSize;
// Don't allow a compiler generated function to overwrite a user
// function in the VTable. Not sure why this happens, but a function
// named __vecDelDtor sometimes shows up on top of the destructor.
if (Func->isCompilerGenerated() && VTableFuncs[Index])
continue;
VTableFuncs[Index] = std::move(Func);
}
}
}
}
UDTLayoutBase::UDTLayoutBase(const PDBSymbol &Symbol, const std::string &Name,
uint32_t Size)
: SymbolBase(Symbol), Name(Name), SizeOf(Size) {
UsedBytes.resize(Size);
ChildrenPerByte.resize(Size);
initializeChildren(Symbol);
}
ClassLayout::ClassLayout(const PDBSymbolTypeUDT &UDT)
: UDTLayoutBase(UDT, UDT.getName(), UDT.getLength()), UDT(UDT) {}
ClassLayout::ClassLayout(std::unique_ptr<PDBSymbolTypeUDT> UDT)
: ClassLayout(*UDT) {
OwnedStorage = std::move(UDT);
}
BaseClassLayout::BaseClassLayout(const UDTLayoutBase &Parent,
std::unique_ptr<PDBSymbolTypeBaseClass> Base)
: UDTLayoutBase(*Base, Base->getName(), Base->getLength()),
StorageItemBase(Parent, *Base, Base->getName(), Base->getOffset(),
Base->getLength()),
Base(std::move(Base)) {
IsVirtualBase = this->Base->isVirtualBaseClass();
}
uint32_t UDTLayoutBase::shallowPaddingSize() const {
return UsedBytes.size() - UsedBytes.count();
}
uint32_t UDTLayoutBase::deepPaddingSize() const {
uint32_t Result = shallowPaddingSize();
for (auto &Child : ChildStorage)
Result += Child->deepPaddingSize();
return Result;
}
void UDTLayoutBase::initializeChildren(const PDBSymbol &Sym) {
auto Children = Sym.findAllChildren();
while (auto Child = Children->getNext()) {
if (auto Data = unique_dyn_cast<PDBSymbolData>(Child)) {
if (Data->getDataKind() == PDB_DataKind::Member) {
auto DM =
llvm::make_unique<DataMemberLayoutItem>(*this, std::move(Data));
addChildToLayout(std::move(DM));
} else {
NonStorageItems.push_back(std::move(Data));
}
continue;
}
if (auto Base = unique_dyn_cast<PDBSymbolTypeBaseClass>(Child)) {
auto BL = llvm::make_unique<BaseClassLayout>(*this, std::move(Base));
BaseClasses.push_back(BL.get());
addChildToLayout(std::move(BL));
continue;
}
if (auto VT = unique_dyn_cast<PDBSymbolTypeVTable>(Child)) {
auto VTLayout = llvm::make_unique<VTableLayoutItem>(*this, std::move(VT));
VTable = VTLayout.get();
addChildToLayout(std::move(VTLayout));
continue;
}
NonStorageItems.push_back(std::move(Child));
}
}
void UDTLayoutBase::addChildToLayout(std::unique_ptr<StorageItemBase> Child) {
uint32_t Begin = Child->getOffsetInParent();
uint32_t End = Begin + Child->getSize();
UsedBytes.set(Begin, End);
while (Begin != End) {
ChildrenPerByte[Begin].push_back(Child.get());
++Begin;
}
auto Loc = std::upper_bound(
ChildStorage.begin(), ChildStorage.end(), Begin,
[](uint32_t Off, const std::unique_ptr<StorageItemBase> &Item) {
return Off < Item->getOffsetInParent();
});
ChildStorage.insert(Loc, std::move(Child));
}
|