Skip to content

Commit

Permalink
[llvm-debuginfo-analyzer] Add support for DW_FORM_implicit_const.
Browse files Browse the repository at this point in the history
The ELF reader ignored attributes with the DW_FORM_implicit_const
form, which caused logical elements to have missing logical data
(mainly line/file declaration numbers).

The issue is described by
  llvm/llvm-project#57040

where GCC generates the following abbreviations:

.debug_abbrev contents:
Abbrev table for offset: 0x00000000
[1] DW_TAG_formal_parameter     DW_CHILDREN_no
        DW_AT_decl_file DW_FORM_implicit_const  1
        DW_AT_decl_line DW_FORM_implicit_const  2

[2] DW_TAG_typedef      DW_CHILDREN_no
        DW_AT_decl_file DW_FORM_implicit_const  1
        DW_AT_decl_line DW_FORM_data1
  • Loading branch information
CarlosAlbertoEnciso committed Aug 17, 2022
1 parent afa4475 commit 6581419
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
32 changes: 18 additions & 14 deletions llvm/lib/DebugInfo/LogicalView/Readers/LVELFReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ void LVELFReader::processOneAttribute(const DWARFDie &Die, LVOffset *OffsetPtr,
const DWARFFormValue &FormValue =
DWARFFormValue::createFromUnit(AttrSpec.Form, U, OffsetPtr);

// We are processing .debug_info section, implicit_const attribute
// values are not really stored here, but in .debug_abbrev section.
auto GetAsUnsignedConstant = [&]() -> int64_t {
return AttrSpec.isImplicitConst() ? AttrSpec.getImplicitConstValue()
: *FormValue.getAsUnsignedConstant();
};

auto GetFlag = [](const DWARFFormValue &FormValue) -> bool {
return FormValue.isFormClass(DWARFFormValue::FC_Flag);
};
Expand Down Expand Up @@ -293,12 +300,12 @@ void LVELFReader::processOneAttribute(const DWARFDie &Die, LVOffset *OffsetPtr,
CurrentElement->setBitSize(*FormValue.getAsUnsignedConstant());
break;
case dwarf::DW_AT_call_file:
CurrentElement->setCallFilenameIndex(*FormValue.getAsUnsignedConstant());
CurrentElement->setCallFilenameIndex(GetAsUnsignedConstant());
break;
case dwarf::DW_AT_call_line:
CurrentElement->setCallLineNumber(
IncrementFileIndex ? *FormValue.getAsUnsignedConstant() + 1
: *FormValue.getAsUnsignedConstant());
CurrentElement->setCallLineNumber(IncrementFileIndex
? GetAsUnsignedConstant() + 1
: GetAsUnsignedConstant());
break;
case dwarf::DW_AT_comp_dir:
CompileUnit->setCompilationDirectory(dwarf::toStringRef(FormValue));
Expand Down Expand Up @@ -331,12 +338,12 @@ void LVELFReader::processOneAttribute(const DWARFDie &Die, LVOffset *OffsetPtr,
CurrentElement->setCount(*FormValue.getAsUnsignedConstant());
break;
case dwarf::DW_AT_decl_line:
CurrentElement->setLineNumber(*FormValue.getAsUnsignedConstant());
CurrentElement->setLineNumber(GetAsUnsignedConstant());
break;
case dwarf::DW_AT_decl_file:
CurrentElement->setFilenameIndex(
IncrementFileIndex ? *FormValue.getAsUnsignedConstant() + 1
: *FormValue.getAsUnsignedConstant());
CurrentElement->setFilenameIndex(IncrementFileIndex
? GetAsUnsignedConstant() + 1
: GetAsUnsignedConstant());
break;
case dwarf::DW_AT_enum_class:
if (GetFlag(FormValue))
Expand Down Expand Up @@ -573,12 +580,9 @@ LVScope *LVELFReader::processOneDie(const DWARFDie &InputDIE, LVScope *Parent,
if (abbrCode) {
if (const DWARFAbbreviationDeclaration *AbbrevDecl =
TheDIE.getAbbreviationDeclarationPtr())
for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec :
AbbrevDecl->attributes())
if (AttrSpec.Form != dwarf::DW_FORM_implicit_const)
// We are processing .debug_info section, implicit_const
// attribute values are not really stored here, but in
// .debug_abbrev section. So we just skip such attrs.
if (AbbrevDecl)
for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec :
AbbrevDecl->attributes())
processOneAttribute(TheDIE, &CurrentEndOffset, AttrSpec);
}
};
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
; Ignored attributes with DW_FORM_implicit_const.
; https://github.com/llvm/llvm-project/issues/57040

; Output generated by g++ (Debian 11.3.0-3) 11.3.0

; .debug_abbrev contents:
; [1] DW_TAG_formal_parameter DW_CHILDREN_no
; DW_AT_decl_file DW_FORM_implicit_const 1
; DW_AT_decl_line DW_FORM_implicit_const 2

; [2] DW_TAG_typedef DW_CHILDREN_no
; DW_AT_decl_file DW_FORM_implicit_const 1
; DW_AT_decl_line DW_FORM_data1

; Attributes with DW_FORM_implicit_const being ignored by the ELFReader,
; causing {Parameter} and {TypeAlias} to omit line numbers.

; test.cpp
; 1 using INTPTR = const int *;
; 2 int foo(INTPTR ParamPtr, unsigned ParamUnsigned, bool ParamBool) {
; 3 if (ParamBool) {
; 4 typedef int INTEGER;
; 5 const INTEGER CONSTANT = 7;
; 6 return CONSTANT;
; 7 }
; 8 return ParamUnsigned;
; 9 }

; RUN: llvm-debuginfo-analyzer --attribute=level,format,producer \
; RUN: --print=scopes,symbols,types \
; RUN: %p/Inputs/pr-57040-test-dwarf-clang.o 2>&1 | \
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s

; ONE: Logical View:
; ONE-NEXT: [000] {File} 'pr-57040-test-dwarf-clang.o' -> elf64-x86-64
; ONE-EMPTY:
; ONE-NEXT: [001] {CompileUnit} 'test.cpp'
; ONE-NEXT: [002] {Producer} 'clang version 14.0.6'
; ONE-NEXT: [002] 1 {TypeAlias} 'INTPTR' -> '* const int'
; ONE-NEXT: [002] 2 {Function} extern not_inlined 'foo' -> 'int'
; ONE-NEXT: [003] {Block}
; ONE-NEXT: [004] 5 {Variable} 'CONSTANT' -> 'const INTEGER'
; ONE-NEXT: [003] 2 {Parameter} 'ParamBool' -> 'bool'
; ONE-NEXT: [003] 2 {Parameter} 'ParamPtr' -> 'INTPTR'
; ONE-NEXT: [003] 2 {Parameter} 'ParamUnsigned' -> 'unsigned int'
; ONE-NEXT: [003] 4 {TypeAlias} 'INTEGER' -> 'int'

; RUN: llvm-debuginfo-analyzer --attribute=level,format,producer \
; RUN: --print=scopes,symbols,types \
; RUN: %p/Inputs/pr-57040-test-dwarf-gcc.o 2>&1 | \
; RUN: FileCheck --strict-whitespace -check-prefix=TWO %s

; TWO: Logical View:
; TWO-NEXT: [000] {File} 'pr-57040-test-dwarf-gcc.o' -> elf64-x86-64
; TWO-EMPTY:
; TWO-NEXT: [001] {CompileUnit} 'test.cpp'
; TWO-NEXT: [002] {Producer} 'GNU C++17 11.3.0 {{.*}}'
; TWO-NEXT: [002] 1 {TypeAlias} 'INTPTR' -> '* const int'
; TWO-NEXT: [002] 2 {Function} extern not_inlined 'foo' -> 'int'
; TWO-NEXT: [003] {Block}
; TWO-NEXT: [004] 4 {TypeAlias} 'INTEGER' -> 'int'
; TWO-NEXT: [004] 5 {Variable} 'CONSTANT' -> 'const INTEGER'
; TWO-NEXT: [003] 2 {Parameter} 'ParamBool' -> 'bool'
; TWO-NEXT: [003] 2 {Parameter} 'ParamPtr' -> 'INTPTR'
; TWO-NEXT: [003] 2 {Parameter} 'ParamUnsigned' -> 'unsigned int'

0 comments on commit 6581419

Please sign in to comment.