public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Clear upper bits during sign extension
@ 2014-12-29  1:13 Yao Qi
  2014-12-29  3:07 ` Joel Brobecker
  2014-12-29 10:48 ` Pedro Alves
  0 siblings, 2 replies; 13+ messages in thread
From: Yao Qi @ 2014-12-29  1:13 UTC (permalink / raw)
  To: gdb-patches

I see the error message "access outside bounds of object referenced
via synthetic pointer" in the two fails below of mips gdb testing

print d[-2]^M
access outside bounds of object referenced via synthetic pointer^M
(gdb) FAIL: gdb.dwarf2/implptrconst.exp: print d[-2]
(gdb) print/d p[-1]^M
access outside bounds of object referenced via synthetic pointer^M
(gdb) FAIL: gdb.dwarf2/implptrpiece.exp: print/d p[-1]

in the first test, 'd[-2]' is processed by GDB as '* (&d[-2])'.  'd'
is a synthetic pointer, so its value is zero, the address of 'd[-2]'
is -2.  In dwarf2loc.c:indirect_pieced_value,

  /* This is an offset requested by GDB, such as value subscripts.
     However, due to how synthetic pointers are implemented, this is
     always presented to us as a pointer type.  This means we have to
     sign-extend it manually as appropriate.  */
  byte_offset = value_as_address (value);                  <---- [1]
  if (TYPE_LENGTH (value_type (value)) < sizeof (LONGEST))
    byte_offset = gdb_sign_extend (byte_offset,            <---- [2]
				   8 * TYPE_LENGTH (value_type (value)));
  byte_offset += piece->v.ptr.offset;

on MIPS target, after [1], byte_offset is -2 (0xfffffffffffffffe),
because 32-bit -2 (as an address) is sign extended to 64-bit.  After
[2], we manually sign extend byte_offset too, and then it becomes
0xfffffffefffffffe, which is wrong.  Function gdb_sign_extend
sign-extends VALUE on bit BIT, and assumes upper bits from bit BIT are
all zero.  That is why the code works well on targets on which address
is zero extended, such as x86.  On these targets, byte_offset is
0xfffffffe (zero extended from 32-bit address -2).

The patch is to clear upper bits of VALUE in gdb_sign_extend first.
Regression tested on mips-linux-gnu, and fixes two fails above.

gdb:

2014-12-29  Yao Qi  <yao@codesourcery.com>

	* utils.c (gdb_sign_extend): Clear bits from BIT in VALUE.
---
 gdb/utils.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/gdb/utils.c b/gdb/utils.c
index 47adb67..e029863 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3031,6 +3031,15 @@ gdb_sign_extend (LONGEST value, int bit)
   if (((value >> (bit - 1)) & 1) != 0)
     {
       LONGEST signbit = ((LONGEST) 1) << (bit - 1);
+      LONGEST mask = 1;
+      int i;
+
+      /* Generate a mask in which bits [0, BIT - 1] are one.  */
+      for (i = 0; i < bit; i++)
+	mask = mask << 1;
+      mask--;
+      /* Clear bits from bit BIT.  */
+      value &= mask;
 
       value = (value ^ signbit) - signbit;
     }
-- 
1.9.3

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-01-08 13:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-29  1:13 [PATCH] Clear upper bits during sign extension Yao Qi
2014-12-29  3:07 ` Joel Brobecker
2014-12-29  3:38   ` Yao Qi
2014-12-29  3:53     ` Joel Brobecker
2014-12-29  5:29     ` Doug Evans
2014-12-29  6:27       ` Yao Qi
2014-12-29 10:48 ` Pedro Alves
2014-12-30  9:20   ` Yao Qi
2014-12-30 12:20     ` Pedro Alves
2014-12-30 13:47       ` Yao Qi
2015-01-08  5:40         ` Yao Qi
2015-01-08 10:42           ` Pedro Alves
2015-01-08 13:06             ` Yao Qi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).