From 20a5fcbd5b28cca88511ac5a9ad5e54251e8fa6d Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 23 Sep 2020 09:39:24 -0600 Subject: Handle bit offset and bit size in base types PR symtab/25470 points out that the Zig programming language allows integers of various bit sizes (including zero), not just sizes that are a multiple of 8. This is supported in DWARF by applying both a byte size and a DW_AT_bit_size. This patch adds support for this feature to integer and boolean types. Other base types are not handled -- for floating-point types, this didn't seem to make sense, and for character types I didn't see much need. (These can be added later if desired.) I've also added support for DW_AT_data_bit_offset at the same time. I don't know whether the Zig compiler requires this, but it was described in the same section in the DWARF standard and was easy to add. A new test case is supplied, using the DWARF assembler. gdb/ChangeLog 2020-09-23 Tom Tromey PR symtab/25470: * value.c (unpack_long, pack_long, pack_unsigned_long): Handle bit offset and bit size. * printcmd.c (print_scalar_formatted): Handle zero-length integer. (print_scalar_formatted): Use bit_size_differs_p. * gdbtypes.h (enum type_specific_kind) : New constant. (union type_specific): : New member. (struct type) : New methods. * gdbtypes.c (init_integer_type, init_boolean_type): Initialize TYPE_SPECIFIC_FIELD. (recursive_dump_type, copy_type_recursive): Update. * dwarf2/read.c (read_base_type): Handle DW_AT_bit_size and DW_AT_data_bit_offset. gdb/testsuite/ChangeLog 2020-09-23 Tom Tromey * gdb.dwarf2/intbits.exp: New file. * gdb.dwarf2/intbits.c: New file. --- gdb/value.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'gdb/value.c') diff --git a/gdb/value.c b/gdb/value.c index 4b28213..7c36c31 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -2776,10 +2776,27 @@ unpack_long (struct type *type, const gdb_byte *valaddr) case TYPE_CODE_MEMBERPTR: { LONGEST result; - if (nosign) - result = extract_unsigned_integer (valaddr, len, byte_order); + + if (type->bit_size_differs_p ()) + { + unsigned bit_off = type->bit_offset (); + unsigned bit_size = type->bit_size (); + if (bit_size == 0) + { + /* unpack_bits_as_long doesn't handle this case the + way we'd like, so handle it here. */ + result = 0; + } + else + result = unpack_bits_as_long (type, valaddr, bit_off, bit_size); + } else - result = extract_signed_integer (valaddr, len, byte_order); + { + if (nosign) + result = extract_unsigned_integer (valaddr, len, byte_order); + else + result = extract_signed_integer (valaddr, len, byte_order); + } if (code == TYPE_CODE_RANGE) result += type->bounds ()->bias; return result; @@ -3339,6 +3356,13 @@ pack_long (gdb_byte *buf, struct type *type, LONGEST num) case TYPE_CODE_FLAGS: case TYPE_CODE_BOOL: case TYPE_CODE_MEMBERPTR: + if (type->bit_size_differs_p ()) + { + unsigned bit_off = type->bit_offset (); + unsigned bit_size = type->bit_size (); + num &= ((ULONGEST) 1 << bit_size) - 1; + num <<= bit_off; + } store_signed_integer (buf, len, byte_order, num); break; @@ -3381,6 +3405,13 @@ pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num) case TYPE_CODE_BOOL: case TYPE_CODE_RANGE: case TYPE_CODE_MEMBERPTR: + if (type->bit_size_differs_p ()) + { + unsigned bit_off = type->bit_offset (); + unsigned bit_size = type->bit_size (); + num &= ((ULONGEST) 1 << bit_size) - 1; + num <<= bit_off; + } store_unsigned_integer (buf, len, byte_order, num); break; -- cgit v1.1