public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Andreas Arnez <arnez@linux.vnet.ibm.com>
To: binutils@sourceware.org
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>,
	       Andreas Krebbel <krebbel@linux.vnet.ibm.com>
Subject: [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands
Date: Fri, 25 Jul 2014 17:02:00 -0000	[thread overview]
Message-ID: <1406307713-7926-3-git-send-email-arnez@linux.vnet.ibm.com> (raw)
In-Reply-To: <1406307713-7926-1-git-send-email-arnez@linux.vnet.ibm.com>

Currently the S/390 disassembler relies on implementation-defined
behavior upon integer overflow and sometimes uses the "%i" printf
format for unsigned values.  This patch intends to fix these.

opcodes/
	* s390-dis.c (union operand_value): New.
	(s390_extract_operand): Change return type to union operand_value.
	Also avoid integer overflow in sign-extension.
	(s390_print_insn_with_opcode): Adjust to changed return value from
	s390_extract_operand().  Change "%i" printf format to "%u" for
	unsigned values.
---
 opcodes/s390-dis.c | 55 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/opcodes/s390-dis.c b/opcodes/s390-dis.c
index 444e7ae..47c449a 100644
--- a/opcodes/s390-dis.c
+++ b/opcodes/s390-dis.c
@@ -92,16 +92,23 @@ s390_insn_matches_opcode (const bfd_byte *buffer,
     && (buffer[5] & opcode->mask[5]) == opcode->opcode[5];
 }
 
+union operand_value
+{
+  int i;
+  unsigned int u;
+};
+
 /* Extracts an operand value from an instruction.  */
 /* We do not perform the shift operation for larl-type address
    operands here since that would lead to an overflow of the 32 bit
    integer value.  Instead the shift operation is done when printing
    the operand.  */
 
-static inline unsigned int
+static inline union operand_value
 s390_extract_operand (const bfd_byte *insn,
 		      const struct s390_operand *operand)
 {
+  union operand_value ret;
   unsigned int val;
   int bits;
 
@@ -123,15 +130,24 @@ s390_extract_operand (const bfd_byte *insn,
   if (operand->bits == 20 && operand->shift == 20)
     val = (val & 0xff) << 12 | (val & 0xfff00) >> 8;
 
-  /* Sign extend value if the operand is signed or pc relative.  */
-  if ((operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
-      && (val & (1U << (operand->bits - 1))))
-    val |= (-1U << (operand->bits - 1)) << 1;
+  /* Sign extend value if the operand is signed or pc relative.  Avoid
+     integer overflows.  */
+  if (operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
+    {
+      unsigned int m = 1U << (operand->bits - 1);
+
+      if (val >= m)
+	ret.i = (int) (val - m) - 1 - (int) (m - 1U);
+      else
+	ret.i = (int) val;
+    }
+  else if (operand->flags & S390_OPERAND_LENGTH)
+    /* Length x in an instruction has real length x + 1.  */
+    ret.u = val + 1;
+  else
+    ret.u = val;
 
-  /* Length x in an instructions has real length x + 1.  */
-  if (operand->flags & S390_OPERAND_LENGTH)
-    val++;
-  return val;
+  return ret;
 }
 
 /* Print the S390 instruction in BUFFER, assuming that it matches the
@@ -156,12 +172,12 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
   for (opindex = opcode->operands; *opindex != 0; opindex++)
     {
       const struct s390_operand *operand = s390_operands + *opindex;
-      unsigned int value = s390_extract_operand (buffer, operand);
+      union operand_value val = s390_extract_operand (buffer, operand);
 
-      if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
+      if ((operand->flags & S390_OPERAND_INDEX) && val.u == 0)
 	continue;
       if ((operand->flags & S390_OPERAND_BASE) &&
-	  value == 0 && separator == '(')
+	  val.u == 0 && separator == '(')
 	{
 	  separator = ',';
 	  continue;
@@ -171,20 +187,19 @@ s390_print_insn_with_opcode (bfd_vma memaddr,
 	(*info->fprintf_func) (info->stream, "%c", separator);
 
       if (operand->flags & S390_OPERAND_GPR)
-	(*info->fprintf_func) (info->stream, "%%r%i", value);
+	(*info->fprintf_func) (info->stream, "%%r%u", val.u);
       else if (operand->flags & S390_OPERAND_FPR)
-	(*info->fprintf_func) (info->stream, "%%f%i", value);
+	(*info->fprintf_func) (info->stream, "%%f%u", val.u);
       else if (operand->flags & S390_OPERAND_AR)
-	(*info->fprintf_func) (info->stream, "%%a%i", value);
+	(*info->fprintf_func) (info->stream, "%%a%u", val.u);
       else if (operand->flags & S390_OPERAND_CR)
-	(*info->fprintf_func) (info->stream, "%%c%i", value);
+	(*info->fprintf_func) (info->stream, "%%c%u", val.u);
       else if (operand->flags & S390_OPERAND_PCREL)
-	(*info->print_address_func) (memaddr + (int)value + (int)value,
-				     info);
+	(*info->print_address_func) (memaddr + val.i + val.i, info);
       else if (operand->flags & S390_OPERAND_SIGNED)
-	(*info->fprintf_func) (info->stream, "%i", (int) value);
+	(*info->fprintf_func) (info->stream, "%i", val.i);
       else
-	(*info->fprintf_func) (info->stream, "%u", value);
+	(*info->fprintf_func) (info->stream, "%u", val.u);
 
       if (operand->flags & S390_OPERAND_DISP)
 	{
-- 
1.8.4.2

  reply	other threads:[~2014-07-25 17:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 17:02 [PATCH 0/6] S/390: Some disassembler cleanup Andreas Arnez
2014-07-25 17:02 ` Andreas Arnez [this message]
2014-07-25 17:02 ` [PATCH 6/6] S/390: Various minor simplifications in disassembler Andreas Arnez
2014-07-25 17:02 ` [PATCH 1/6] S/390: Split disassembler routine into smaller functions Andreas Arnez
2014-07-25 17:02 ` [PATCH 5/6] S/390: Drop function pointer dereferences in disassembler Andreas Arnez
2014-07-25 17:02 ` [PATCH 4/6] S/390: Simplify opcode search loop " Andreas Arnez
2014-07-25 17:02 ` [PATCH 3/6] S/390: Fix off-by-one error in disassembler initialization Andreas Arnez
2014-08-19 14:43 ` [PATCH 0/6] S/390: Some disassembler cleanup Nicholas Clifton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1406307713-7926-3-git-send-email-arnez@linux.vnet.ibm.com \
    --to=arnez@linux.vnet.ibm.com \
    --cc=binutils@sourceware.org \
    --cc=krebbel@linux.vnet.ibm.com \
    --cc=schwidefsky@de.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).