summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/DebugInfo
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/unittests/DebugInfo')
-rw-r--r--llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp93
-rw-r--r--llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp11
2 files changed, 101 insertions, 3 deletions
diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
index 0d299c9d840..1be0363adb0 100644
--- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
+++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
@@ -1007,6 +1007,99 @@ TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
TestAddresses<4, AddrType>();
}
+TEST(DWARFDebugInfo, TestStringOffsets) {
+ Triple Triple = getHostTripleForAddrSize(sizeof(void *));
+ if (!isConfigurationSupported(Triple))
+ return;
+
+ const char *String1 = "Hello";
+ const char *String2 = "World";
+
+ auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
+ ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
+ dwarfgen::Generator *DG = ExpectedDG.get().get();
+ dwarfgen::CompileUnit &CU = DG->addCompileUnit();
+ dwarfgen::DIE CUDie = CU.getUnitDIE();
+
+ CUDie.addStrOffsetsBaseAttribute();
+
+ uint16_t Attr = DW_AT_lo_user;
+
+ // Create our strings. First we create a non-indexed reference to String1,
+ // followed by an indexed String2. Finally, we add an indexed reference to
+ // String1.
+ const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
+
+ const auto Attr2 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr2, DW_FORM_strx, String2);
+
+ const auto Attr3 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr3, DW_FORM_strx, String1);
+
+ // Generate the DWARF
+ StringRef FileBytes = DG->generate();
+ MemoryBufferRef FileBuffer(FileBytes, "dwarf");
+ auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
+ ASSERT_TRUE((bool)Obj);
+ std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
+ uint32_t NumCUs = DwarfContext->getNumCompileUnits();
+ ASSERT_EQ(NumCUs, 1u);
+ DWARFUnit *U = DwarfContext->getUnitAtIndex(0);
+ auto DieDG = U->getUnitDIE(false);
+ ASSERT_TRUE(DieDG.isValid());
+
+ // Now make sure the string offsets came out properly. Attr2 should have index
+ // 0 (because it was the first indexed string) even though the string itself
+ // was added eariler.
+ auto Extracted1 = toString(DieDG.find(Attr1));
+ ASSERT_TRUE((bool)Extracted1);
+ EXPECT_STREQ(String1, *Extracted1);
+
+ Optional<DWARFFormValue> Form2 = DieDG.find(Attr2);
+ ASSERT_TRUE((bool)Form2);
+ EXPECT_EQ(0u, Form2->getRawUValue());
+ auto Extracted2 = toString(Form2);
+ ASSERT_TRUE((bool)Extracted2);
+ EXPECT_STREQ(String2, *Extracted2);
+
+ Optional<DWARFFormValue> Form3 = DieDG.find(Attr3);
+ ASSERT_TRUE((bool)Form3);
+ EXPECT_EQ(1u, Form3->getRawUValue());
+ auto Extracted3 = toString(Form3);
+ ASSERT_TRUE((bool)Extracted3);
+ EXPECT_STREQ(String1, *Extracted3);
+}
+
+TEST(DWARFDebugInfo, TestEmptyStringOffsets) {
+ Triple Triple = getHostTripleForAddrSize(sizeof(void *));
+ if (!isConfigurationSupported(Triple))
+ return;
+
+ const char *String1 = "Hello";
+
+ auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
+ ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
+ dwarfgen::Generator *DG = ExpectedDG.get().get();
+ dwarfgen::CompileUnit &CU = DG->addCompileUnit();
+ dwarfgen::DIE CUDie = CU.getUnitDIE();
+
+ uint16_t Attr = DW_AT_lo_user;
+
+ // We shall insert only one string. It will be referenced directly.
+ const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
+
+ // Generate the DWARF
+ StringRef FileBytes = DG->generate();
+ MemoryBufferRef FileBuffer(FileBytes, "dwarf");
+ auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
+ ASSERT_TRUE((bool)Obj);
+ std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
+ EXPECT_TRUE(
+ DwarfContext->getDWARFObj().getStringOffsetSection().Data.empty());
+}
+
TEST(DWARFDebugInfo, TestRelations) {
Triple Triple = getHostTripleForAddrSize(sizeof(void *));
if (!isConfigurationSupported(Triple))
diff --git a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
index adc400c2fd0..973276616b8 100644
--- a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
+++ b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
@@ -71,15 +71,20 @@ void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
break;
case DW_FORM_strp:
+ Die->addValue(
+ DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
+ DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String)));
+ break;
+
case DW_FORM_GNU_str_index:
case DW_FORM_strx:
case DW_FORM_strx1:
case DW_FORM_strx2:
case DW_FORM_strx3:
case DW_FORM_strx4:
- Die->addValue(
- DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
- DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String)));
+ Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
+ DIEString(DG.getStringPool().getIndexedEntry(
+ *DG.getAsmPrinter(), String)));
break;
default:
OpenPOWER on IntegriCloud