llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[BOLT] DWARF 5: DW_AT_byte_size for DW_TAG_pointer_type is incorrectly reset to 0 during debug section update

Open not4juu opened this issue 7 months ago • 3 comments

Description

While using llvm-bolt with --update-debug-sections on a binary compiled with -gdwarf-5, BOLT incorrectly transforms the DW_AT_byte_size attribute for DW_TAG_pointer_type.

In the original binary (compiled with g++ -gdwarf-5), the pointer type correctly uses DW_FORM_implicit_const with a value of 8. After processing with BOLT, the value in the rewritten .debug_abbrev section is changed to 0 and puts DW_AT_byte_size to 0 as well. This issue does not occur when using -gdwarf-4.

Reproduction Steps

Can use simple app like:

#include <memory>
#include <iostream>
#include <exception>
#include <cstdlib>

class C
{
public:
    C(int i) : val{i} {};
    void abort() { if (val == 7) std::terminate(); };
    void print() { std::cout << val << "\n"; }

private:
    int val;
};

int main(int /*argc*/, char* argv[])
{
    std::unique_ptr<C> c = std::make_unique<C>(std::atoi(argv[1]));
    c->print();
    c->abort();
}

Compile with DWARF 5 and DWARF 4 for comparison:

g++  main.cpp -g -gdwarf-5 -gz=none -o main_v5
g++  main.cpp -g -gdwarf-4 -gz=none -o main_v4

Run BOLT with debug section updates:

llvm-bolt main_v4 -o main_v4.bolt -update-debug-sections
llvm-bolt main_v5 -o main_v5.bolt -update-debug-sections

Observed Results

  1. DWARF 4 (Correct behavior)
readelf --debug-dump=info main_v4.bolt | grep -A2 DW_TAG_pointer_type
<1><60ed>: Abbrev Number: 103 (DW_TAG_pointer_type)
<60ee>   DW_AT_byte_size   : 8
<60ef>   DW_AT_type        : <0x38fb>

readelf --debug-dump=abbrev main_v4.bolt | less
looking into abbrev number 103:

103      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_data1
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0
  1. DWARF 5 - Original Binary (Correct behavior)
readelf --debug-dump=info main_v5 | grep -A2 DW_TAG_pointer_type
<1><5efd>: Abbrev Number: 6 (DW_TAG_pointer_type)
<5efe>   DW_AT_byte_size   : 8
<5efe>   DW_AT_type        : <0x4612>

readelf --debug-dump=abbrev main_v5| less
looking into abbrev number 6:

6      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_implicit_const: 8
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0
  1. DWARF 5 - After BOLT (Incorrect behavior)
readelf --debug-dump=info main_v5.bolt | grep -A2 DW_TAG_pointer_type
<1><5eaf>: Abbrev Number: 91 (DW_TAG_pointer_type)
<5eb0>   DW_AT_byte_size   : 0
<5eb0>   DW_AT_type        : <0x3a27>

readelf --debug-dump=abbrev main_v5.bolt | less
looking into abbrev number 91:

91      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_implicit_const: 0
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0

Environment

  1. OS: Rocky Linux 10.1 (Red Quartz)
  2. Architecture: x86_64
  3. Binary Info: ELF 64-bit LSB executable, x86-64, not stripped
  4. LLVM/BOLT version: 23.0.0git (Commit: a057097d590601bdefc3c99baa784a82973bea53)
  5. GCC version: 14.2.0
  6. Linker: GNU ld (GNU Binutils) 2.46

The start point of investigation indicates: https://github.com/llvm/llvm-project/blob/main/bolt/lib/Core/DIEBuilder.cpp#L836 as under #843 value 0 is assigned but even that manipulation with AttrSpec value in lib after llvm-bolt usage byte size was not updated correctly so perhaps the may be problem also in rewriting part of debug sections.

Thank you in advance for your time

not4juu avatar Apr 14 '26 16:04 not4juu

@llvm/issue-subscribers-bolt

Author: Sebastian (not4juu)

**Description**

While using llvm-bolt with --update-debug-sections on a binary compiled with -gdwarf-5, BOLT incorrectly transforms the DW_AT_byte_size attribute for DW_TAG_pointer_type.

In the original binary (compiled with g++ -gdwarf-5), the pointer type correctly uses DW_FORM_implicit_const with a value of 8. After processing with BOLT, the value in the rewritten .debug_abbrev section is changed to 0 and puts DW_AT_byte_size to 0 as well. This issue does not occur when using -gdwarf-4.

Reproduction Steps

Can use simple app like:

#include &lt;memory&gt;
#include &lt;iostream&gt;
#include &lt;exception&gt;
#include &lt;cstdlib&gt;

class C
{
public:
    C(int i) : val{i} {};
    void abort() { if (val == 7) std::terminate(); };
    void print() { std::cout &lt;&lt; val &lt;&lt; "\n"; }

private:
    int val;
};

int main(int /*argc*/, char* argv[])
{
    std::unique_ptr&lt;C&gt; c = std::make_unique&lt;C&gt;(std::atoi(argv[1]));
    c-&gt;print();
    c-&gt;abort();
}

Compile with DWARF 5 and DWARF 4 for comparison:

g++  main.cpp -g -gdwarf-5 -gz=none -o main_v5
g++  main.cpp -g -gdwarf-4 -gz=none -o main_v4

Run BOLT with debug section updates:

llvm-bolt main_v4 -o main_v4.bolt -update-debug-sections
llvm-bolt main_v5 -o main_v5.bolt -update-debug-sections

Observed Results

  1. DWARF 4 (Correct behavior)
readelf --debug-dump=info main_v4.bolt | grep -A2 DW_TAG_pointer_type
&lt;1&gt;&lt;60ed&gt;: Abbrev Number: 103 (DW_TAG_pointer_type)
&lt;60ee&gt;   DW_AT_byte_size   : 8
&lt;60ef&gt;   DW_AT_type        : &lt;0x38fb&gt;

readelf --debug-dump=abbrev main_v4.bolt | less
looking into abbrev number 103:

103      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_data1
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0
  1. DWARF 5 - Original Binary (Correct behavior)
readelf --debug-dump=info main_v5 | grep -A2 DW_TAG_pointer_type
&lt;1&gt;&lt;5efd&gt;: Abbrev Number: 6 (DW_TAG_pointer_type)
&lt;5efe&gt;   DW_AT_byte_size   : 8
&lt;5efe&gt;   DW_AT_type        : &lt;0x4612&gt;

readelf --debug-dump=abbrev main_v5| less
looking into abbrev number 6:

6      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_implicit_const: 8
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0
  1. DWARF 5 - After BOLT (Incorrect behavior)
readelf --debug-dump=info main_v5.bolt | grep -A2 DW_TAG_pointer_type
&lt;1&gt;&lt;5eaf&gt;: Abbrev Number: 91 (DW_TAG_pointer_type)
&lt;5eb0&gt;   DW_AT_byte_size   : 0
&lt;5eb0&gt;   DW_AT_type        : &lt;0x3a27&gt;

readelf --debug-dump=abbrev main_v5.bolt | less
looking into abbrev number 91:

91      DW_TAG_pointer_type    [no children]
DW_AT_byte_size    DW_FORM_implicit_const: 0
DW_AT_type         DW_FORM_ref4
DW_AT value: 0     DW_FORM value: 0

Environment

  1. OS: Rocky Linux 10.1 (Red Quartz)
  2. Architecture: x86_64
  3. Binary Info: ELF 64-bit LSB executable, x86-64, not stripped
  4. LLVM/BOLT version: 23.0.0git (Commit: a057097d590601bdefc3c99baa784a82973bea53)
  5. GCC version: 14.2.0
  6. Linker: GNU ld (GNU Binutils) 2.46

The start point of investigation indicates: https://github.com/llvm/llvm-project/blob/main/bolt/lib/Core/DIEBuilder.cpp#L836 as under #843 value 0 is assigned but even that manipulation with AttrSpec value in lib after llvm-bolt usage byte size was not updated correctly so perhaps the may be problem also in rewriting part of debug sections.

Thank you in advance for your time

llvmbot avatar Apr 14 '26 16:04 llvmbot

@not4juu can you confirm that the PR by @rafaelauler works for you, either here or at the PR?

paschalis-mpeis avatar May 08 '26 08:05 paschalis-mpeis

Hello @paschalis-mpeis indeed that fix now does not reset now DW_AT_byte_size and DW_FORM_implicit_const to 0 I see that values for simple app now are correctly updated. I confirm it works for me.

not4juu avatar May 21 '26 13:05 not4juu