Enumeration constants emitted with the wrong sign/extension
| Bugzilla Link | 19712 |
| Version | trunk |
| OS | All |
| CC | @adrian-prantl,@echristo |
Extended Description
When emitting a debug info constant (DwarfUnit::addConstantValue) we interpret the 64 bits of immediate as signed or unsigned based on the type.
We don't emit the underlying type of an enum, so we just assume the type is one way or the other (DwarfUnit.cpp:isUnsignedDIType - we assume that the type is signed).
This means we get this wrong:
enum x { y = 4294967295U }; x z;
By describing the constant as -1 (assuming the 64 bit constant was signed)
0x0000003b: DW_TAG_enumerator [4]
DW_AT_name [DW_FORM_strp] ( .debug_str[0x0000002e] = "y")
DW_AT_const_value [DW_FORM_sdata] (-1)
FWIW: GCC always produces sdata constants by the looks of it, but it gets it right by producing a larger constant, rather than using a possibly smaller udata (even if the enum's underlying type is an unsigned type) value.
It looks like we do the right thing now: Godbolt link (uses readelf which doesn't show FORMs): https://godbolt.org/z/o6vcP1W19.
Or - see the reproducer below. Clang now emits a udata constant for @dwblaikie's example.
0x00000032: DW_TAG_enumerator [4] (0x00000029)
DW_AT_name [DW_FORM_strx1] (indexed (00000005) string = "y")
DW_AT_const_value [DW_FORM_udata] (4294967295)
Using a clang version 17.00:
I've added another enum a which uses signed 8 byte int for storage for comparison.
enum x { y = 4294967295U, z = (unsigned)-1 }; // byte size 4
x X;
enum a { b = 4294967295U, c = -1 }; // byte size 8
a A;
$ clang test.cpp -g -c -o test.o
$ llvm-dwarfdump -v test.o --name x --show-children
test.o: file format elf64-x86-64
0x00000029: DW_TAG_enumeration_type [3] * (0x0000000c)
DW_AT_type [DW_FORM_ref4] (cu + 0x0041 => {0x00000041} "unsigned int")
DW_AT_name [DW_FORM_strx1] (indexed (00000007) string = "x")
DW_AT_byte_size [DW_FORM_data1] (0x04)
DW_AT_decl_file [DW_FORM_data1] ("test.cpp")
DW_AT_decl_line [DW_FORM_data1] (1)
0x00000032: DW_TAG_enumerator [4] (0x00000029)
DW_AT_name [DW_FORM_strx1] (indexed (00000005) string = "y")
DW_AT_const_value [DW_FORM_udata] (4294967295)
0x00000039: DW_TAG_enumerator [4] (0x00000029)
DW_AT_name [DW_FORM_strx1] (indexed (00000006) string = "z")
DW_AT_const_value [DW_FORM_udata] (4294967295)
0x00000040: NULL
$ llvm-dwarfdump -v test.o --name a --show-children
test.o: file format elf64-x86-64
0x00000050: DW_TAG_enumeration_type [3] * (0x0000000c)
DW_AT_type [DW_FORM_ref4] (cu + 0x0064 => {0x00000064} "long")
DW_AT_name [DW_FORM_strx1] (indexed (0000000c) string = "a")
DW_AT_byte_size [DW_FORM_data1] (0x08)
DW_AT_decl_file [DW_FORM_data1] ("test.cpp")
DW_AT_decl_line [DW_FORM_data1] (4)
0x00000059: DW_TAG_enumerator [6] (0x00000050)
DW_AT_name [DW_FORM_strx1] (indexed (0000000a) string = "b")
DW_AT_const_value [DW_FORM_sdata] (4294967295)
0x00000060: DW_TAG_enumerator [6] (0x00000050)
DW_AT_name [DW_FORM_strx1] (indexed (0000000b) string = "c")
DW_AT_const_value [DW_FORM_sdata] (-1)
0x00000063: NULL