[llvm] [llvm-dwp] Fix infinite loop on DWARFv5 DW_FORM_implicit_const (PR #205567)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 25 05:08:13 PDT 2026
https://github.com/youngd007 updated https://github.com/llvm/llvm-project/pull/205567
>From 803f47385f3bcc8380d269992ee84819b9787c4c Mon Sep 17 00:00:00 2001
From: David Young <davidayoung at meta.com>
Date: Wed, 24 Jun 2026 06:58:29 -0700
Subject: [PATCH 1/5] [llvm-dwp] Fix infinite loop on DWARFv5
DW_FORM_implicit_const
---
llvm/lib/DWP/DWP.cpp | 28 +++++++--
llvm/test/tools/llvm-dwp/X86/implicit_const.s | 58 +++++++++++++++++++
2 files changed, 80 insertions(+), 6 deletions(-)
create mode 100644 llvm/test/tools/llvm-dwp/X86/implicit_const.s
diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp
index db0d33cd5a52d..779c9ea7f371c 100644
--- a/llvm/lib/DWP/DWP.cpp
+++ b/llvm/lib/DWP/DWP.cpp
@@ -48,8 +48,18 @@ static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
// DW_CHILDREN
AbbrevData.getU8(&Offset);
// Attributes
- while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
- ;
+ while (true) {
+ uint64_t Name = AbbrevData.getULEB128(&Offset);
+ dwarf::Form Form =
+ static_cast<dwarf::Form>(AbbrevData.getULEB128(&Offset));
+ // DW_FORM_implicit_const carries its value as an SLEB128 in the
+ // abbreviation declaration itself; failing to consume it desyncs the
+ // rest of the abbreviation table walk.
+ if (Form == dwarf::DW_FORM_implicit_const)
+ AbbrevData.getSLEB128(&Offset);
+ if (Name == 0 && Form == 0)
+ break;
+ }
}
return Offset;
}
@@ -115,10 +125,16 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
AbbrevData.getU8(&AbbrevOffset);
uint32_t Name;
dwarf::Form Form;
- while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
- (Form = static_cast<dwarf::Form>(
- AbbrevData.getULEB128(&AbbrevOffset))) &&
- (Name != 0 || Form != 0)) {
+ for (;;) {
+ Name = AbbrevData.getULEB128(&AbbrevOffset);
+ Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset));
+ // DW_FORM_implicit_const stores its value as an SLEB128 in the abbreviation
+ // declaration, not in .debug_info; consume it so the (attribute, form) walk
+ // stays aligned.
+ if (Form == dwarf::DW_FORM_implicit_const)
+ AbbrevData.getSLEB128(&AbbrevOffset);
+ if (Name == 0 && Form == 0)
+ break;
switch (Name) {
case dwarf::DW_AT_name: {
Expected<const char *> EName = getIndexedString(
diff --git a/llvm/test/tools/llvm-dwp/X86/implicit_const.s b/llvm/test/tools/llvm-dwp/X86/implicit_const.s
new file mode 100644
index 0000000000000..15d9569c7c671
--- /dev/null
+++ b/llvm/test/tools/llvm-dwp/X86/implicit_const.s
@@ -0,0 +1,58 @@
+# Check that llvm-dwp correctly handles a DWARFv5 .dwo whose abbreviation table
+# uses DW_FORM_implicit_const. DW_FORM_implicit_const stores its value as an
+# SLEB128 in the abbreviation declaration itself (not in .debug_info). If
+# llvm-dwp fails to consume that value while scanning the abbreviation table, the
+# (attribute, form) walk desyncs; here the desync makes the scan for the
+# compile-unit's abbreviation code run off the end of .debug_abbrev and spin
+# forever. This is a regression test: before the fix llvm-dwp hangs, after the
+# fix it terminates and emits the expected CU identifiers.
+
+# RUN: llvm-mc --triple=x86_64-unknown-linux --filetype=obj --split-dwarf-file=%t.dwo -dwarf-version=5 %s -o %t.o
+# RUN: llvm-dwp %t.dwo -o %t.dwp
+# RUN: llvm-dwarfdump -v %t.dwp | FileCheck %s
+
+# CHECK: .debug_info.dwo contents:
+# CHECK: 0x00000000: Compile Unit: {{.*}} version = 0x0005, unit_type = DW_UT_split_compile, {{.*}} DWO_id = [[DWOID:0x[0-9a-f]+]]
+# CHECK: DW_TAG_compile_unit
+# CHECK: DW_AT_name{{.*}}"test.c"
+# CHECK: DW_AT_dwo_name{{.*}}"test.dwo"
+
+# CHECK: .debug_cu_index contents:
+# CHECK: version = 5, units = 1, slots = 2
+# CHECK: [[DWOID]] [0x0000000000000000
+
+ .section .debug_info.dwo,"e", at progbits
+ .long .Ldebug_info_dwo_end0-.Ldebug_info_dwo_start0 # Length of Unit
+.Ldebug_info_dwo_start0:
+ .short 5 # DWARF version number
+ .byte 5 # DWARF Unit Type (DW_UT_split_compile)
+ .byte 8 # Address Size (in bytes)
+ .long 0 # Offset Into Abbrev. Section
+ .quad 0x1100001122334455 # DWO_id
+ .byte 2 # Abbrev [2] DW_TAG_compile_unit
+ .asciz "test.c" # DW_AT_name
+ .asciz "test.dwo" # DW_AT_dwo_name
+.Ldebug_info_dwo_end0:
+
+ .section .debug_abbrev.dwo,"e", at progbits
+ # Abbrev [1] DW_TAG_variable, declared *before* the compile unit and using
+ # DW_FORM_implicit_const. This is the declaration that desyncs the walk.
+ .byte 1 # Abbreviation Code
+ .byte 52 # DW_TAG_variable
+ .byte 0 # DW_CHILDREN_no
+ .byte 28 # DW_AT_const_value
+ .byte 33 # DW_FORM_implicit_const
+ .byte 42 # implicit const value (SLEB128)
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ # Abbrev [2] DW_TAG_compile_unit
+ .byte 2 # Abbreviation Code
+ .byte 17 # DW_TAG_compile_unit
+ .byte 0 # DW_CHILDREN_no
+ .byte 3 # DW_AT_name
+ .byte 8 # DW_FORM_string
+ .byte 118 # DW_AT_dwo_name
+ .byte 8 # DW_FORM_string
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 0 # EOM(3)
>From 2768aceddcdf263af6b2f0a2b54c31c1b51396d7 Mon Sep 17 00:00:00 2001
From: David Young <davidayoung at meta.com>
Date: Wed, 24 Jun 2026 11:19:02 -0700
Subject: [PATCH 2/5] Refactor to helper to avoid code duplication for dwp fix
---
llvm/lib/DWP/DWP.cpp | 46 ++++++++++++++++++++++----------------------
1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp
index 779c9ea7f371c..550af68ddfad8 100644
--- a/llvm/lib/DWP/DWP.cpp
+++ b/llvm/lib/DWP/DWP.cpp
@@ -39,6 +39,19 @@ static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,
return 8; // unit length: 4 bytes, version: 2 bytes, padding: 2 bytes.
}
+// Read the next (attribute, form) pair from an abbreviation declaration.
+// DW_FORM_implicit_const is the only form that stores an extra value -- an
+// SLEB128 -- in the abbreviation declaration itself; consume it here so the
+// (attribute, form) walk stays aligned with the rest of the table.
+static void readAbbrevAttribute(const DataExtractor &AbbrevData,
+ uint64_t *Offset, uint64_t &Name,
+ dwarf::Form &Form) {
+ Name = AbbrevData.getULEB128(Offset);
+ Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
+ if (Form == dwarf::DW_FORM_implicit_const)
+ AbbrevData.getSLEB128(Offset);
+}
+
static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
uint64_t Offset = 0;
DataExtractor AbbrevData(Abbrev, true);
@@ -48,18 +61,12 @@ static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
// DW_CHILDREN
AbbrevData.getU8(&Offset);
// Attributes
- while (true) {
- uint64_t Name = AbbrevData.getULEB128(&Offset);
- dwarf::Form Form =
- static_cast<dwarf::Form>(AbbrevData.getULEB128(&Offset));
- // DW_FORM_implicit_const carries its value as an SLEB128 in the
- // abbreviation declaration itself; failing to consume it desyncs the
- // rest of the abbreviation table walk.
- if (Form == dwarf::DW_FORM_implicit_const)
- AbbrevData.getSLEB128(&Offset);
- if (Name == 0 && Form == 0)
- break;
- }
+ uint64_t Name;
+ dwarf::Form Form;
+ for (readAbbrevAttribute(AbbrevData, &Offset, Name, Form);
+ Name != 0 || Form != 0;
+ readAbbrevAttribute(AbbrevData, &Offset, Name, Form))
+ ;
}
return Offset;
}
@@ -123,18 +130,11 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
return make_error<DWPError>("top level DIE is not a compile unit");
// DW_CHILDREN
AbbrevData.getU8(&AbbrevOffset);
- uint32_t Name;
+ uint64_t Name;
dwarf::Form Form;
- for (;;) {
- Name = AbbrevData.getULEB128(&AbbrevOffset);
- Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset));
- // DW_FORM_implicit_const stores its value as an SLEB128 in the abbreviation
- // declaration, not in .debug_info; consume it so the (attribute, form) walk
- // stays aligned.
- if (Form == dwarf::DW_FORM_implicit_const)
- AbbrevData.getSLEB128(&AbbrevOffset);
- if (Name == 0 && Form == 0)
- break;
+ for (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form);
+ Name != 0 || Form != 0;
+ readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form)) {
switch (Name) {
case dwarf::DW_AT_name: {
Expected<const char *> EName = getIndexedString(
>From 6508d0ff43bbe7c5d7af073278c4ba5ba8a13262 Mon Sep 17 00:00:00 2001
From: David Young <davidayoung at meta.com>
Date: Wed, 24 Jun 2026 11:54:56 -0700
Subject: [PATCH 3/5] Refactor readAbbrevAttribute to return bool if can keep
going (Name/Form is not zero)
---
llvm/lib/DWP/DWP.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp
index 550af68ddfad8..a7f174e22b3f9 100644
--- a/llvm/lib/DWP/DWP.cpp
+++ b/llvm/lib/DWP/DWP.cpp
@@ -43,13 +43,17 @@ static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,
// DW_FORM_implicit_const is the only form that stores an extra value -- an
// SLEB128 -- in the abbreviation declaration itself; consume it here so the
// (attribute, form) walk stays aligned with the rest of the table.
-static void readAbbrevAttribute(const DataExtractor &AbbrevData,
+// return True if can keep going- Name or Form is not 0
+static bool readAbbrevAttribute(const DataExtractor &AbbrevData,
uint64_t *Offset, uint64_t &Name,
dwarf::Form &Form) {
Name = AbbrevData.getULEB128(Offset);
Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
if (Form == dwarf::DW_FORM_implicit_const)
AbbrevData.getSLEB128(Offset);
+ if (Name != 0 || Form != 0)
+ return true;
+ return false;
}
static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
@@ -63,9 +67,7 @@ static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
// Attributes
uint64_t Name;
dwarf::Form Form;
- for (readAbbrevAttribute(AbbrevData, &Offset, Name, Form);
- Name != 0 || Form != 0;
- readAbbrevAttribute(AbbrevData, &Offset, Name, Form))
+ while (readAbbrevAttribute(AbbrevData, &Offset, Name, Form))
;
}
return Offset;
@@ -132,9 +134,7 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
AbbrevData.getU8(&AbbrevOffset);
uint64_t Name;
dwarf::Form Form;
- for (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form);
- Name != 0 || Form != 0;
- readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form)) {
+ while (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form)) {
switch (Name) {
case dwarf::DW_AT_name: {
Expected<const char *> EName = getIndexedString(
>From 4ca0bb68e7321857762419bccc5d97eb66de9778 Mon Sep 17 00:00:00 2001
From: David Young <davidayoung at meta.com>
Date: Wed, 24 Jun 2026 14:54:37 -0700
Subject: [PATCH 4/5] Add tests for dwo_id using the implicit_const value and
outer loop not finding a value and erroring now
---
llvm/lib/DWP/DWP.cpp | 56 +++++++++++--------
.../llvm-dwp/X86/abbrev_code_not_found.s | 29 ++++++++++
.../llvm-dwp/X86/implicit_const_dwo_id.s | 37 ++++++++++++
3 files changed, 100 insertions(+), 22 deletions(-)
create mode 100644 llvm/test/tools/llvm-dwp/X86/abbrev_code_not_found.s
create mode 100644 llvm/test/tools/llvm-dwp/X86/implicit_const_dwo_id.s
diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp
index a7f174e22b3f9..746ae1f33a826 100644
--- a/llvm/lib/DWP/DWP.cpp
+++ b/llvm/lib/DWP/DWP.cpp
@@ -39,38 +39,43 @@ static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,
return 8; // unit length: 4 bytes, version: 2 bytes, padding: 2 bytes.
}
-// Read the next (attribute, form) pair from an abbreviation declaration.
-// DW_FORM_implicit_const is the only form that stores an extra value -- an
-// SLEB128 -- in the abbreviation declaration itself; consume it here so the
-// (attribute, form) walk stays aligned with the rest of the table.
-// return True if can keep going- Name or Form is not 0
+// Read the next (attribute, form) pair into Name and Form, also consuming the
+// inline DW_FORM_implicit_const value when present. Returns false on the
+// terminating (0, 0) pair.
static bool readAbbrevAttribute(const DataExtractor &AbbrevData,
- uint64_t *Offset, uint64_t &Name,
- dwarf::Form &Form) {
- Name = AbbrevData.getULEB128(Offset);
+ uint64_t *Offset, dwarf::Attribute &Name,
+ dwarf::Form &Form, int64_t &ImplicitConst) {
+ Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(Offset));
Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
+ ImplicitConst = 0;
if (Form == dwarf::DW_FORM_implicit_const)
- AbbrevData.getSLEB128(Offset);
- if (Name != 0 || Form != 0)
- return true;
- return false;
+ ImplicitConst = AbbrevData.getSLEB128(Offset);
+ return Name != 0 || Form != 0;
}
-static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
+static Expected<uint64_t> getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
uint64_t Offset = 0;
DataExtractor AbbrevData(Abbrev, true);
- while (AbbrevData.getULEB128(&Offset) != AbbrCode) {
+ while (AbbrevData.isValidOffset(Offset)) {
+ uint64_t Code = AbbrevData.getULEB128(&Offset);
+ if (Code == AbbrCode)
+ return Offset;
+ // A zero abbreviation code marks the end of the abbreviation table.
+ if (Code == 0)
+ break;
// Tag
AbbrevData.getULEB128(&Offset);
// DW_CHILDREN
AbbrevData.getU8(&Offset);
- // Attributes
- uint64_t Name;
+ // Attribute specifications, terminated by a (0, 0) pair.
+ dwarf::Attribute Name;
dwarf::Form Form;
- while (readAbbrevAttribute(AbbrevData, &Offset, Name, Form))
+ int64_t ImplicitConst;
+ while (readAbbrevAttribute(AbbrevData, &Offset, Name, Form, ImplicitConst))
;
}
- return Offset;
+ return make_error<DWPError>("abbrev code " + utostr(AbbrCode) +
+ " not found in abbrev section");
}
static Expected<const char *>
@@ -126,15 +131,20 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
uint32_t AbbrCode = InfoData.getULEB128(&Offset);
DataExtractor AbbrevData(Abbrev, true);
- uint64_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
+ Expected<uint64_t> AbbrevOffsetOrErr = getCUAbbrev(Abbrev, AbbrCode);
+ if (!AbbrevOffsetOrErr)
+ return AbbrevOffsetOrErr.takeError();
+ uint64_t AbbrevOffset = *AbbrevOffsetOrErr;
auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
if (Tag != dwarf::DW_TAG_compile_unit)
return make_error<DWPError>("top level DIE is not a compile unit");
// DW_CHILDREN
AbbrevData.getU8(&AbbrevOffset);
- uint64_t Name;
+ dwarf::Attribute Name;
dwarf::Form Form;
- while (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form)) {
+ int64_t ImplicitConst;
+ while (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form,
+ ImplicitConst)) {
switch (Name) {
case dwarf::DW_AT_name: {
Expected<const char *> EName = getIndexedString(
@@ -154,7 +164,9 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
break;
}
case dwarf::DW_AT_GNU_dwo_id:
- Header.Signature = InfoData.getU64(&Offset);
+ Header.Signature = Form == dwarf::DW_FORM_implicit_const
+ ? static_cast<uint64_t>(ImplicitConst)
+ : InfoData.getU64(&Offset);
break;
default:
DWARFFormValue::skipValue(
diff --git a/llvm/test/tools/llvm-dwp/X86/abbrev_code_not_found.s b/llvm/test/tools/llvm-dwp/X86/abbrev_code_not_found.s
new file mode 100644
index 0000000000000..7248337e6ee98
--- /dev/null
+++ b/llvm/test/tools/llvm-dwp/X86/abbrev_code_not_found.s
@@ -0,0 +1,29 @@
+# Check that llvm-dwp reports a clean error (instead of looping forever) when a
+# compile unit references an abbreviation code that is absent from the
+# abbreviation table.
+
+# RUN: llvm-mc --triple=x86_64-unknown-linux --filetype=obj --split-dwarf-file=%t.dwo -dwarf-version=5 %s -o %t.o
+# RUN: not llvm-dwp %t.dwo -o %t.dwp 2>&1 | FileCheck %s
+
+# CHECK: abbrev code 2 not found in abbrev section
+
+ .section .debug_info.dwo,"e", at progbits
+ .long .Ldebug_info_dwo_end0-.Ldebug_info_dwo_start0 # Length of Unit
+.Ldebug_info_dwo_start0:
+ .short 5 # DWARF version number
+ .byte 5 # DWARF Unit Type (DW_UT_split_compile)
+ .byte 8 # Address Size (in bytes)
+ .long 0 # Offset Into Abbrev. Section
+ .quad 0x1100001122334455 # DWO_id
+ .byte 2 # Abbrev [2] -- not present in the table below
+.Ldebug_info_dwo_end0:
+
+ .section .debug_abbrev.dwo,"e", at progbits
+ .byte 1 # Abbreviation Code
+ .byte 17 # DW_TAG_compile_unit
+ .byte 0 # DW_CHILDREN_no
+ .byte 3 # DW_AT_name
+ .byte 8 # DW_FORM_string
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 0 # EOM(3) -- end of abbreviation table
diff --git a/llvm/test/tools/llvm-dwp/X86/implicit_const_dwo_id.s b/llvm/test/tools/llvm-dwp/X86/implicit_const_dwo_id.s
new file mode 100644
index 0000000000000..d5ca17b9117ec
--- /dev/null
+++ b/llvm/test/tools/llvm-dwp/X86/implicit_const_dwo_id.s
@@ -0,0 +1,37 @@
+# Check that llvm-dwp reads DW_AT_GNU_dwo_id from the abbreviation table when it
+# is encoded with DW_FORM_implicit_const (the value is stored inline in the
+# abbreviation, not in .debug_info). The dwo_id must round-trip into the
+# .debug_cu_index signature.
+
+# RUN: llvm-mc -triple x86_64-unknown-linux %s -filetype=obj -o %t.o -split-dwarf-file=%t.dwo -dwarf-version=4
+# RUN: llvm-dwp %t.dwo -o %t.dwp
+# RUN: llvm-dwarfdump -debug-info -debug-cu-index %t.dwp | FileCheck %s
+
+# CHECK: .debug_info.dwo contents:
+# CHECK: DW_AT_GNU_dwo_id{{.*}}(305419896)
+
+# CHECK: .debug_cu_index contents:
+# CHECK: version = 2, units = 1, slots = 2
+# CHECK: 0x0000000012345678 [0x0000000000000000
+
+ .section .debug_info.dwo,"e", at progbits
+ .long .Ldebug_info_dwo_end0-.Ldebug_info_dwo_start0 # Length of Unit
+.Ldebug_info_dwo_start0:
+ .short 4 # DWARF version number
+ .long 0 # Offset Into Abbrev. Section
+ .byte 8 # Address Size (in bytes)
+ .byte 1 # Abbrev [1] DW_TAG_compile_unit
+ # DW_AT_GNU_dwo_id has no value here; it is
+ # carried inline by DW_FORM_implicit_const.
+.Ldebug_info_dwo_end0:
+
+ .section .debug_abbrev.dwo,"e", at progbits
+ .byte 1 # Abbreviation Code
+ .byte 17 # DW_TAG_compile_unit
+ .byte 0 # DW_CHILDREN_no
+ .ascii "\261B" # DW_AT_GNU_dwo_id (ULEB 0x2161)
+ .byte 33 # DW_FORM_implicit_const
+ .sleb128 305419896 # implicit const value (0x12345678)
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 0 # EOM(3)
>From 8987f31595df5889f0e82ecdda4b09d6c63282eb Mon Sep 17 00:00:00 2001
From: David Young <davidayoung at meta.com>
Date: Thu, 25 Jun 2026 05:07:53 -0700
Subject: [PATCH 5/5] Refactor helper from DWP to Dwarf file, add unit test for
it, and int->optional<int>
---
.../llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h | 10 ++++
llvm/lib/DWP/DWP.cpp | 25 ++-------
llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp | 12 ++++
.../DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp | 56 +++++++++++++++++++
4 files changed, 84 insertions(+), 19 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
index 28c33c02a69d4..63b168f732b02 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
@@ -14,12 +14,22 @@
#include "llvm/Support/DataExtractor.h"
#include <cstdint>
#include <map>
+#include <optional>
#include <vector>
namespace llvm {
class raw_ostream;
+/// Read the next (attribute, form) specification from an abbreviation
+/// declaration at \p Offset, advancing \p Offset past it. \p ImplicitConst is
+/// set to the inline value of a DW_FORM_implicit_const attribute and to
+/// std::nullopt otherwise. Returns false on the terminating (0, 0) pair.
+LLVM_ABI bool readAbbrevAttribute(const DataExtractor &AbbrevData,
+ uint64_t *Offset, dwarf::Attribute &Name,
+ dwarf::Form &Form,
+ std::optional<int64_t> &ImplicitConst);
+
class DWARFAbbreviationDeclarationSet {
uint64_t Offset;
/// Code of the first abbreviation, if all abbreviations in the set have
diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp
index 746ae1f33a826..c678163928f7a 100644
--- a/llvm/lib/DWP/DWP.cpp
+++ b/llvm/lib/DWP/DWP.cpp
@@ -17,12 +17,14 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/DWP/DWPError.h"
#include "llvm/DWP/ELFWriter.h"
+#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/Object/Decompressor.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include <limits>
+#include <optional>
using namespace llvm;
using namespace llvm::object;
@@ -39,20 +41,6 @@ static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,
return 8; // unit length: 4 bytes, version: 2 bytes, padding: 2 bytes.
}
-// Read the next (attribute, form) pair into Name and Form, also consuming the
-// inline DW_FORM_implicit_const value when present. Returns false on the
-// terminating (0, 0) pair.
-static bool readAbbrevAttribute(const DataExtractor &AbbrevData,
- uint64_t *Offset, dwarf::Attribute &Name,
- dwarf::Form &Form, int64_t &ImplicitConst) {
- Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(Offset));
- Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
- ImplicitConst = 0;
- if (Form == dwarf::DW_FORM_implicit_const)
- ImplicitConst = AbbrevData.getSLEB128(Offset);
- return Name != 0 || Form != 0;
-}
-
static Expected<uint64_t> getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
uint64_t Offset = 0;
DataExtractor AbbrevData(Abbrev, true);
@@ -70,7 +58,7 @@ static Expected<uint64_t> getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
// Attribute specifications, terminated by a (0, 0) pair.
dwarf::Attribute Name;
dwarf::Form Form;
- int64_t ImplicitConst;
+ std::optional<int64_t> ImplicitConst;
while (readAbbrevAttribute(AbbrevData, &Offset, Name, Form, ImplicitConst))
;
}
@@ -142,7 +130,7 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
AbbrevData.getU8(&AbbrevOffset);
dwarf::Attribute Name;
dwarf::Form Form;
- int64_t ImplicitConst;
+ std::optional<int64_t> ImplicitConst;
while (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form,
ImplicitConst)) {
switch (Name) {
@@ -164,9 +152,8 @@ getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
break;
}
case dwarf::DW_AT_GNU_dwo_id:
- Header.Signature = Form == dwarf::DW_FORM_implicit_const
- ? static_cast<uint64_t>(ImplicitConst)
- : InfoData.getU64(&Offset);
+ Header.Signature = ImplicitConst ? static_cast<uint64_t>(*ImplicitConst)
+ : InfoData.getU64(&Offset);
break;
default:
DWARFFormValue::skipValue(
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp
index 47553c562f4cd..d58add4442d2b 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugAbbrev.cpp
@@ -15,6 +15,18 @@
using namespace llvm;
+bool llvm::readAbbrevAttribute(const DataExtractor &AbbrevData,
+ uint64_t *Offset, dwarf::Attribute &Name,
+ dwarf::Form &Form,
+ std::optional<int64_t> &ImplicitConst) {
+ Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(Offset));
+ Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
+ ImplicitConst = std::nullopt;
+ if (Form == dwarf::DW_FORM_implicit_const)
+ ImplicitConst = AbbrevData.getSLEB128(Offset);
+ return Name != 0 || Form != 0;
+}
+
DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() {
clear();
}
diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp
index 02b91f097e030..be4b0db1d9996 100644
--- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp
+++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp
@@ -437,3 +437,59 @@ TEST(DWARFDebugAbbrevTest, DWARFDebugAbbrevParseError) {
"abbreviation declaration attribute list was not terminated with a "
"null entry"));
}
+
+TEST(DWARFDebugAbbrevTest, ReadAbbrevAttributeRegular) {
+ SmallString<16> RawData;
+ raw_svector_ostream OS(RawData);
+ encodeULEB128(DW_AT_name, OS);
+ encodeULEB128(DW_FORM_strp, OS);
+
+ DataExtractor Data(RawData, sys::IsLittleEndianHost);
+ uint64_t Offset = 0;
+ dwarf::Attribute Name;
+ dwarf::Form Form;
+ std::optional<int64_t> ImplicitConst;
+ EXPECT_TRUE(readAbbrevAttribute(Data, &Offset, Name, Form, ImplicitConst));
+ EXPECT_EQ(Name, DW_AT_name);
+ EXPECT_EQ(Form, DW_FORM_strp);
+ EXPECT_FALSE(ImplicitConst.has_value());
+ // The attribute and form were consumed; no inline value follows.
+ EXPECT_EQ(Offset, RawData.size());
+}
+
+TEST(DWARFDebugAbbrevTest, ReadAbbrevAttributeImplicitConst) {
+ SmallString<16> RawData;
+ raw_svector_ostream OS(RawData);
+ encodeULEB128(DW_AT_const_value, OS);
+ encodeULEB128(DW_FORM_implicit_const, OS);
+ encodeSLEB128(-42, OS);
+
+ DataExtractor Data(RawData, sys::IsLittleEndianHost);
+ uint64_t Offset = 0;
+ dwarf::Attribute Name;
+ dwarf::Form Form;
+ std::optional<int64_t> ImplicitConst;
+ EXPECT_TRUE(readAbbrevAttribute(Data, &Offset, Name, Form, ImplicitConst));
+ EXPECT_EQ(Name, DW_AT_const_value);
+ EXPECT_EQ(Form, DW_FORM_implicit_const);
+ ASSERT_TRUE(ImplicitConst.has_value());
+ EXPECT_EQ(*ImplicitConst, -42);
+ // The inline SLEB128 value was consumed in addition to the (attribute, form)
+ // pair.
+ EXPECT_EQ(Offset, RawData.size());
+}
+
+TEST(DWARFDebugAbbrevTest, ReadAbbrevAttributeTerminator) {
+ SmallString<16> RawData;
+ raw_svector_ostream OS(RawData);
+ encodeULEB128(0, OS);
+ encodeULEB128(0, OS);
+
+ DataExtractor Data(RawData, sys::IsLittleEndianHost);
+ uint64_t Offset = 0;
+ dwarf::Attribute Name;
+ dwarf::Form Form;
+ std::optional<int64_t> ImplicitConst;
+ EXPECT_FALSE(readAbbrevAttribute(Data, &Offset, Name, Form, ImplicitConst));
+ EXPECT_FALSE(ImplicitConst.has_value());
+}
More information about the llvm-commits
mailing list