Skip to content

Commit 4c697cf

Browse files
committed
[llvm-debuginfo-analyzer] Add support for DWARF DW_AT_byte_size
1 parent 3bc3b1c commit 4c697cf

File tree

8 files changed

+77
-1
lines changed

8 files changed

+77
-1
lines changed

llvm/include/llvm/DebugInfo/LogicalView/Core/LVElement.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ using LVElementKindSet = std::set<LVElementKind>;
6464
using LVElementDispatch = std::map<LVElementKind, LVElementGetFunction>;
6565
using LVElementRequest = std::vector<LVElementGetFunction>;
6666

67+
// Assume 8-bit bytes; this is consistent, e.g. with
68+
// lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp.
69+
constexpr unsigned int DWARF_CHAR_BIT = 8u;
70+
6771
class LVElement : public LVObject {
6872
enum class Property {
6973
IsLine, // A logical line.
@@ -239,6 +243,9 @@ class LVElement : public LVObject {
239243
virtual bool isBase() const { return false; }
240244
virtual bool isTemplateParam() const { return false; }
241245

246+
uint32_t getStorageSizeInBytes() const {
247+
return (getBitSize() + (DWARF_CHAR_BIT - 1)) / DWARF_CHAR_BIT;
248+
}
242249
virtual uint32_t getBitSize() const { return 0; }
243250
virtual void setBitSize(uint32_t Size) {}
244251

llvm/include/llvm/DebugInfo/LogicalView/Core/LVScope.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class LVScope : public LVElement {
9393
LVProperties<Property> Properties;
9494
static LVScopeDispatch Dispatch;
9595

96+
// Size in bits if this scope represents also a compound type.
97+
uint32_t BitSize = 0;
98+
9699
// Coverage factor in units (bytes).
97100
unsigned CoverageFactor = 0;
98101

@@ -269,6 +272,9 @@ class LVScope : public LVElement {
269272
bool removeElement(LVElement *Element) override;
270273
void updateLevel(LVScope *Parent, bool Moved) override;
271274

275+
uint32_t getBitSize() const override { return BitSize; }
276+
void setBitSize(uint32_t Size) override { BitSize = Size; }
277+
272278
void resolve() override;
273279
void resolveName() override;
274280
void resolveReferences() override;

llvm/include/llvm/DebugInfo/LogicalView/Core/LVType.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class LVType : public LVElement {
5656
LVProperties<Property> Properties;
5757
static LVTypeDispatch Dispatch;
5858

59+
// Size in bits of a symbol of this type.
60+
uint32_t BitSize = 0;
61+
5962
// Find the current type in the given 'Targets'.
6063
LVType *findIn(const LVTypes *Targets) const;
6164

@@ -109,6 +112,10 @@ class LVType : public LVElement {
109112
virtual LVElement *getUnderlyingType() { return nullptr; }
110113
virtual void setUnderlyingType(LVElement *Element) {}
111114

115+
// Return the size in bits of an entity of this type.
116+
uint32_t getBitSize() const override { return BitSize; }
117+
void setBitSize(uint32_t Size) override { BitSize = Size; }
118+
112119
void resolveName() override;
113120
void resolveReferences() override;
114121

llvm/lib/DebugInfo/LogicalView/Core/LVType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ void LVType::print(raw_ostream &OS, bool Full) const {
292292
}
293293

294294
void LVType::printExtra(raw_ostream &OS, bool Full) const {
295-
OS << formattedKind(kind()) << " " << formattedName(getName()) << "\n";
295+
OS << formattedKind(kind()) << " " << formattedName(getName());
296+
if (uint32_t Size = getStorageSizeInBytes())
297+
OS << " [Size = " << Size << "]";
298+
OS << "\n";
296299
}
297300

298301
//===----------------------------------------------------------------------===//

llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewVisitor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, ClassRecord &Class,
19871987
Scope->setName(Class.getName());
19881988
if (Class.hasUniqueName())
19891989
Scope->setLinkageName(Class.getUniqueName());
1990+
Scope->setBitSize(Class.getSize() * DWARF_CHAR_BIT);
19901991

19911992
if (Class.isNested()) {
19921993
Scope->setIsNested();
@@ -2455,6 +2456,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, UnionRecord &Union,
24552456
Scope->setName(Union.getName());
24562457
if (Union.hasUniqueName())
24572458
Scope->setLinkageName(Union.getUniqueName());
2459+
Scope->setBitSize(Union.getSize() * DWARF_CHAR_BIT);
24582460

24592461
if (Union.isNested()) {
24602462
Scope->setIsNested();
@@ -3208,6 +3210,7 @@ LVType *LVLogicalVisitor::createBaseType(TypeIndex TI, StringRef TypeName) {
32083210

32093211
if (createElement(TIR, SimpleKind)) {
32103212
CurrentType->setName(TypeName);
3213+
CurrentType->setBitSize(getSizeInBytesForTypeIndex(TIR) * DWARF_CHAR_BIT);
32113214
Reader->getCompileUnit()->addElement(CurrentType);
32123215
}
32133216
return CurrentType;

llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ void LVDWARFReader::processOneAttribute(const DWARFDie &Die,
307307
case dwarf::DW_AT_bit_size:
308308
CurrentElement->setBitSize(GetAsUnsignedConstant());
309309
break;
310+
case dwarf::DW_AT_byte_size:
311+
CurrentElement->setBitSize(GetAsUnsignedConstant() * DWARF_CHAR_BIT);
312+
break;
310313
case dwarf::DW_AT_call_file:
311314
CurrentElement->setCallFilenameIndex(IncrementFileIndex
312315
? GetAsUnsignedConstant() + 1

llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Testing/Support/Error.h"
2222

2323
#include "gtest/gtest.h"
24+
#include <algorithm>
2425

2526
using namespace llvm;
2627
using namespace llvm::logicalview;
@@ -128,6 +129,26 @@ void checkElementPropertiesClangCodeview(LVReader *Reader) {
128129
const LVLines *Lines = Foo->getLines();
129130
ASSERT_NE(Lines, nullptr);
130131
EXPECT_EQ(Lines->size(), 0x10u);
132+
133+
// Check size of types in CompileUnit.
134+
const LVTypes *Types = CompileUnit->getTypes();
135+
ASSERT_NE(Types, nullptr);
136+
EXPECT_EQ(Types->size(), 6u);
137+
138+
const auto BoolType =
139+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
140+
return elt->getName() == "bool";
141+
});
142+
ASSERT_NE(BoolType, Types->end());
143+
const auto IntType =
144+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
145+
return elt->getName() == "int";
146+
});
147+
ASSERT_NE(IntType, Types->end());
148+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
149+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
150+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
151+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
131152
}
132153

133154
// Check the logical elements basic properties (MSVC - Codeview).
@@ -194,6 +215,26 @@ void checkElementPropertiesMsvcCodeview(LVReader *Reader) {
194215
const LVLines *Lines = Foo->getLines();
195216
ASSERT_NE(Lines, nullptr);
196217
EXPECT_EQ(Lines->size(), 0x0eu);
218+
219+
// Check size of types in CompileUnit.
220+
const LVTypes *Types = CompileUnit->getTypes();
221+
ASSERT_NE(Types, nullptr);
222+
EXPECT_EQ(Types->size(), 8u);
223+
224+
const auto BoolType =
225+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
226+
return elt->getName() == "bool";
227+
});
228+
ASSERT_NE(BoolType, Types->end());
229+
const auto IntType =
230+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
231+
return elt->getName() == "int";
232+
});
233+
ASSERT_NE(IntType, Types->end());
234+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
235+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
236+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
237+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
197238
}
198239

199240
// Check the logical elements basic properties (MSVC library - Codeview).

llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ void checkElementSelection(LVReader *Reader) {
155155
ASSERT_NE(Element, nullptr);
156156
EXPECT_NE(Element->getName().find("INTEGER"), StringRef::npos);
157157
EXPECT_EQ(Element->getIsType(), 1);
158+
// Underlying type is `int`
159+
const LVElement *UnderlyingType =
160+
static_cast<LVType *>(Element)->getUnderlyingType();
161+
EXPECT_EQ(UnderlyingType->getIsType(), 1);
162+
EXPECT_EQ(UnderlyingType->getBitSize(), 32u);
163+
EXPECT_EQ(UnderlyingType->getStorageSizeInBytes(), 4u);
158164

159165
Element = MapElements[0x000000000f]; // 'movl %edx, %eax'
160166
ASSERT_NE(Element, nullptr);

0 commit comments

Comments
 (0)