Skip to content

Commit fc56a83

Browse files
jalopezg-gitpeledins-zimperium
authored andcommitted
[llvm-debuginfo-analyzer] Add support for DWARF DW_AT_byte_size
1 parent 9684986 commit fc56a83

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
@@ -83,6 +83,10 @@ struct LVSourceLanguage {
8383
Language;
8484
};
8585

86+
// Assume 8-bit bytes; this is consistent, e.g. with
87+
// lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp.
88+
constexpr unsigned int DWARF_CHAR_BIT = 8u;
89+
8690
class LVElement : public LVObject {
8791
enum class Property {
8892
IsLine, // A logical line.
@@ -261,6 +265,9 @@ class LVElement : public LVObject {
261265
virtual bool isBase() const { return false; }
262266
virtual bool isTemplateParam() const { return false; }
263267

268+
uint32_t getStorageSizeInBytes() const {
269+
return (getBitSize() + (DWARF_CHAR_BIT - 1)) / DWARF_CHAR_BIT;
270+
}
264271
virtual uint32_t getBitSize() const { return 0; }
265272
virtual void setBitSize(uint32_t Size) {}
266273

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
@@ -2000,6 +2000,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, ClassRecord &Class,
20002000
Scope->setName(Class.getName());
20012001
if (Class.hasUniqueName())
20022002
Scope->setLinkageName(Class.getUniqueName());
2003+
Scope->setBitSize(Class.getSize() * DWARF_CHAR_BIT);
20032004

20042005
if (Class.isNested()) {
20052006
Scope->setIsNested();
@@ -2468,6 +2469,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, UnionRecord &Union,
24682469
Scope->setName(Union.getName());
24692470
if (Union.hasUniqueName())
24702471
Scope->setLinkageName(Union.getUniqueName());
2472+
Scope->setBitSize(Union.getSize() * DWARF_CHAR_BIT);
24712473

24722474
if (Union.isNested()) {
24732475
Scope->setIsNested();
@@ -3221,6 +3223,7 @@ LVType *LVLogicalVisitor::createBaseType(TypeIndex TI, StringRef TypeName) {
32213223

32223224
if (createElement(TIR, SimpleKind)) {
32233225
CurrentType->setName(TypeName);
3226+
CurrentType->setBitSize(getSizeInBytesForTypeIndex(TIR) * DWARF_CHAR_BIT);
32243227
Reader->getCompileUnit()->addElement(CurrentType);
32253228
}
32263229
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(*FormValue.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;
@@ -133,6 +134,26 @@ void checkElementPropertiesClangCodeview(LVReader *Reader) {
133134
const LVLines *Lines = Foo->getLines();
134135
ASSERT_NE(Lines, nullptr);
135136
EXPECT_EQ(Lines->size(), 0x10u);
137+
138+
// Check size of types in CompileUnit.
139+
const LVTypes *Types = CompileUnit->getTypes();
140+
ASSERT_NE(Types, nullptr);
141+
EXPECT_EQ(Types->size(), 6u);
142+
143+
const auto BoolType =
144+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
145+
return elt->getName() == "bool";
146+
});
147+
ASSERT_NE(BoolType, Types->end());
148+
const auto IntType =
149+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
150+
return elt->getName() == "int";
151+
});
152+
ASSERT_NE(IntType, Types->end());
153+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
154+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
155+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
156+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
136157
}
137158

138159
// Check the logical elements basic properties (MSVC - Codeview).
@@ -199,6 +220,26 @@ void checkElementPropertiesMsvcCodeview(LVReader *Reader) {
199220
const LVLines *Lines = Foo->getLines();
200221
ASSERT_NE(Lines, nullptr);
201222
EXPECT_EQ(Lines->size(), 0x0eu);
223+
224+
// Check size of types in CompileUnit.
225+
const LVTypes *Types = CompileUnit->getTypes();
226+
ASSERT_NE(Types, nullptr);
227+
EXPECT_EQ(Types->size(), 8u);
228+
229+
const auto BoolType =
230+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
231+
return elt->getName() == "bool";
232+
});
233+
ASSERT_NE(BoolType, Types->end());
234+
const auto IntType =
235+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
236+
return elt->getName() == "int";
237+
});
238+
ASSERT_NE(IntType, Types->end());
239+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
240+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
241+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
242+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
202243
}
203244

204245
// 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
@@ -193,6 +193,12 @@ void checkElementSelection(LVReader *Reader) {
193193
ASSERT_NE(Element, nullptr);
194194
EXPECT_NE(Element->getName().find("INTEGER"), StringRef::npos);
195195
EXPECT_EQ(Element->getIsType(), 1);
196+
// Underlying type is `int`
197+
const LVElement *UnderlyingType =
198+
static_cast<LVType *>(Element)->getUnderlyingType();
199+
EXPECT_EQ(UnderlyingType->getIsType(), 1);
200+
EXPECT_EQ(UnderlyingType->getBitSize(), 32u);
201+
EXPECT_EQ(UnderlyingType->getStorageSizeInBytes(), 4u);
196202

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

0 commit comments

Comments
 (0)