Jan Kratochvil 4f1de05
commit d5d1163eff2415a01895f1cff8bbee32b3f0ab66
Jan Kratochvil 4f1de05
Author: Andreas Arnez <arnez@linux.vnet.ibm.com>
Jan Kratochvil 4f1de05
Date:   Tue Jun 13 15:20:26 2017 +0200
Jan Kratochvil 4f1de05
Jan Kratochvil 4f1de05
    write_pieced_value: Fix size capping logic
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    A field f in a structure composed of DWARF pieces may be located in
Jan Kratochvil 4f1de05
    multiple pieces, where the first and last of those may contain bits from
Jan Kratochvil 4f1de05
    other fields as well.  So when writing to f, the beginning of the first
Jan Kratochvil 4f1de05
    and the end of the last of those pieces may have to be skipped.  But the
Jan Kratochvil 4f1de05
    logic in write_pieced_value for handling one of those pieces is flawed
Jan Kratochvil 4f1de05
    when the first and last piece are the same, i.e., f is contained in a
Jan Kratochvil 4f1de05
    single piece:
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
      < - - - - - - - - - piece_size - - - - - - - - - ->
Jan Kratochvil 4f1de05
      +-------------------------------------------------+
Jan Kratochvil 4f1de05
      | skipped_bits |   f_bits   | / / / / / / / / / / |
Jan Kratochvil 4f1de05
      +-------------------------------------------------+
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    The current logic determines the size of the sub-piece to operate on by
Jan Kratochvil 4f1de05
    limiting the piece size to the bit size of f and then subtracting the
Jan Kratochvil 4f1de05
    skipped bits:
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
      min (piece_size, f_bits) - skipped_bits
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    Instead of:
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
      min (piece_size - skipped_bits, f_bits)
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    So the resulting sub-piece size is corrupted, leading to wrong handling of
Jan Kratochvil 4f1de05
    this piece in write_pieced_value.
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    Note that the same bug was already found in read_pieced_value and fixed
Jan Kratochvil 4f1de05
    there (but not in write_pieced_value), see PR 15391.
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    This patch swaps the calculations, bringing them into the same (correct)
Jan Kratochvil 4f1de05
    order as in read_pieced_value.
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    gdb/ChangeLog:
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
            * dwarf2loc.c (write_pieced_value): Fix order of calculations for
Jan Kratochvil 4f1de05
            size capping.
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
    gdb/testsuite/ChangeLog:
Jan Kratochvil 4f1de05
    
Jan Kratochvil 4f1de05
            * gdb.dwarf2/var-pieces.exp: Add test case for modifying a
Jan Kratochvil 4f1de05
            variable at nonzero offset.
Jan Kratochvil 4f1de05
Jan Kratochvil 4f1de05
### a/gdb/ChangeLog
Jan Kratochvil 4f1de05
### b/gdb/ChangeLog
Jan Kratochvil 4f1de05
## -1,3 +1,8 @@
Jan Kratochvil 4f1de05
+2017-06-13  Andreas Arnez  <arnez@linux.vnet.ibm.com>
Jan Kratochvil 4f1de05
+
Jan Kratochvil 4f1de05
+	* dwarf2loc.c (write_pieced_value): Fix order of calculations for
Jan Kratochvil 4f1de05
+	size capping.
Jan Kratochvil 4f1de05
+
Jan Kratochvil 4f1de05
 2017-06-13  Yao Qi  <yao.qi@linaro.org>
Jan Kratochvil 4f1de05
 
Jan Kratochvil 4f1de05
 	* mips-linux-nat.c: Move include features/mips*-linux.c to
Jan Kratochvil 4f1de05
--- a/gdb/dwarf2loc.c
Jan Kratochvil 4f1de05
+++ b/gdb/dwarf2loc.c
Jan Kratochvil 4f1de05
@@ -1964,8 +1964,6 @@ write_pieced_value (struct value *to, struct value *from)
Jan Kratochvil 4f1de05
 	  bits_to_skip -= this_size_bits;
Jan Kratochvil 4f1de05
 	  continue;
Jan Kratochvil 4f1de05
 	}
Jan Kratochvil 4f1de05
-      if (this_size_bits > type_len - offset)
Jan Kratochvil 4f1de05
-	this_size_bits = type_len - offset;
Jan Kratochvil 4f1de05
       if (bits_to_skip > 0)
Jan Kratochvil 4f1de05
 	{
Jan Kratochvil 4f1de05
 	  dest_offset_bits = bits_to_skip;
Jan Kratochvil 4f1de05
@@ -1978,6 +1976,8 @@ write_pieced_value (struct value *to, struct value *from)
Jan Kratochvil 4f1de05
 	  dest_offset_bits = 0;
Jan Kratochvil 4f1de05
 	  source_offset_bits = offset;
Jan Kratochvil 4f1de05
 	}
Jan Kratochvil 4f1de05
+      if (this_size_bits > type_len - offset)
Jan Kratochvil 4f1de05
+	this_size_bits = type_len - offset;
Jan Kratochvil 4f1de05
 
Jan Kratochvil 4f1de05
       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
Jan Kratochvil 4f1de05
       source_offset = source_offset_bits / 8;
Jan Kratochvil 4f1de05
### a/gdb/testsuite/ChangeLog
Jan Kratochvil 4f1de05
### b/gdb/testsuite/ChangeLog
Jan Kratochvil 4f1de05
## -1,5 +1,10 @@
Jan Kratochvil 4f1de05
 2017-06-13  Andreas Arnez  <arnez@linux.vnet.ibm.com>
Jan Kratochvil 4f1de05
 
Jan Kratochvil 4f1de05
+	* gdb.dwarf2/var-pieces.exp: Add test case for modifying a
Jan Kratochvil 4f1de05
+	variable at nonzero offset.
Jan Kratochvil 4f1de05
+
Jan Kratochvil 4f1de05
+2017-06-13  Andreas Arnez  <arnez@linux.vnet.ibm.com>
Jan Kratochvil 4f1de05
+
Jan Kratochvil 4f1de05
 	* gdb.dwarf2/var-access.c: New file.
Jan Kratochvil 4f1de05
 	* gdb.dwarf2/var-access.exp: New test.
Jan Kratochvil 4f1de05
 	* lib/gdb-utils.exp (string_to_regexp): Quote braces as well.
Jan Kratochvil 4f1de05
--- a/gdb/testsuite/gdb.dwarf2/var-access.exp
Jan Kratochvil 4f1de05
+++ b/gdb/testsuite/gdb.dwarf2/var-access.exp
Jan Kratochvil 4f1de05
@@ -178,6 +178,11 @@ gdb_test "print/d s1" " = \\{a = 63, b = 3, c = 0, d = 1\\}" \
Jan Kratochvil 4f1de05
     "verify s1.a"
Jan Kratochvil 4f1de05
 gdb_test "print/d a" " = \\{0, 1, 63, 3, 4, 5, 6, 7\\}" \
Jan Kratochvil 4f1de05
     "verify s1.a through a"
Jan Kratochvil 4f1de05
+gdb_test_no_output "set var s1.b = 42"
Jan Kratochvil 4f1de05
+gdb_test "print/d s1" " = \\{a = 63, b = 42, c = 0, d = 1\\}" \
Jan Kratochvil 4f1de05
+    "verify s1.b"
Jan Kratochvil 4f1de05
+gdb_test "print/d a" " = \\{0, 1, 63, 42, 4, 5, 6, 7\\}" \
Jan Kratochvil 4f1de05
+    "verify s1.b through a"
Jan Kratochvil 4f1de05
 
Jan Kratochvil 4f1de05
 # Byte-aligned register- and memory pieces.
Jan Kratochvil 4f1de05
 gdb_test_no_output "set var \$[lindex $regname 0] = 81" \