[BOLT] DWARF 5: DW_AT_byte_size for DW_TAG_pointer_type is incorrectly reset to 0 during debug section update
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
- 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
- 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
- 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
- OS: Rocky Linux 10.1 (Red Quartz)
- Architecture: x86_64
- Binary Info: ELF 64-bit LSB executable, x86-64, not stripped
- LLVM/BOLT version: 23.0.0git (Commit: a057097d590601bdefc3c99baa784a82973bea53)
- GCC version: 14.2.0
- 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
@llvm/issue-subscribers-bolt
Author: Sebastian (not4juu)
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
- 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
- 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
- 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
- OS: Rocky Linux 10.1 (Red Quartz)
- Architecture: x86_64
- Binary Info: ELF 64-bit LSB executable, x86-64, not stripped
- LLVM/BOLT version: 23.0.0git (Commit: a057097d590601bdefc3c99baa784a82973bea53)
- GCC version: 14.2.0
- 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 can you confirm that the PR by @rafaelauler works for you, either here or at the PR?
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.