From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4680 invoked by alias); 25 Jul 2014 17:02:05 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 4595 invoked by uid 89); 25 Jul 2014 17:02:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: e06smtp16.uk.ibm.com Received: from e06smtp16.uk.ibm.com (HELO e06smtp16.uk.ibm.com) (195.75.94.112) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Fri, 25 Jul 2014 17:02:01 +0000 Received: from /spool/local by e06smtp16.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 25 Jul 2014 18:01:58 +0100 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp16.uk.ibm.com (192.168.101.146) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 25 Jul 2014 18:01:56 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id 4F39417D8045 for ; Fri, 25 Jul 2014 18:03:36 +0100 (BST) Received: from d06av04.portsmouth.uk.ibm.com (d06av04.portsmouth.uk.ibm.com [9.149.37.216]) by b06cxnps4074.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id s6PH1tor22675482 for ; Fri, 25 Jul 2014 17:01:55 GMT Received: from d06av04.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s6PH1sYI023197 for ; Fri, 25 Jul 2014 11:01:55 -0600 Received: from br87z6lw.boeblingen.de.ibm.com (dyn-9-152-212-196.boeblingen.de.ibm.com [9.152.212.196]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id s6PH1r7B023174; Fri, 25 Jul 2014 11:01:54 -0600 From: Andreas Arnez To: binutils@sourceware.org Cc: Martin Schwidefsky , Andreas Krebbel Subject: [PATCH 2/6] S/390: Fix disassembler's treatment of signed/unsigned operands Date: Fri, 25 Jul 2014 17:02:00 -0000 Message-Id: <1406307713-7926-3-git-send-email-arnez@linux.vnet.ibm.com> In-Reply-To: <1406307713-7926-1-git-send-email-arnez@linux.vnet.ibm.com> References: <1406307713-7926-1-git-send-email-arnez@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14072517-3548-0000-0000-000000B3DD11 X-SW-Source: 2014-07/txt/msg00205.txt.bz2 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