config/tc-ppc.c (md_apply_fix): Sign extend fieldval under control of operand flag bits.

Upstream-Status: Backport

Signed-off-by: Zhenhua Luo <zhenhua.luo@freescale.com>

commit cf5434698882f41ddfcc469488e8a68c674aae03
Author: Alan Modra <amodra@bigpond.net.au>
Date:   Thu May 9 04:01:53 2013 +0000

--- binutils-2.23.2/gas/config/tc-ppc.c.orig	2013-06-02 21:53:24.000000000 -0500
+++ binutils-2.23.2/gas/config/tc-ppc.c	2013-06-02 22:03:00.000000000 -0500
@@ -6355,7 +6355,10 @@
 	case BFD_RELOC_LO16_PCREL:
 	case BFD_RELOC_PPC_VLE_LO16A:
 	case BFD_RELOC_PPC_VLE_LO16D:
-	  fieldval = SEX16 (value);
+	  fieldval = value & 0xffff;
+	sign_extend_16:
+	  if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
+	    fieldval = (fieldval ^ 0x8000) - 0x8000;
 	  fixP->fx_no_overflow = 1;
 	  break;
 
@@ -6366,9 +6369,8 @@
 	case BFD_RELOC_HI16_PCREL:
 	case BFD_RELOC_PPC_VLE_HI16A:
 	case BFD_RELOC_PPC_VLE_HI16D:
-	  fieldval = SEX16 (PPC_HI (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HI (value);
+	  goto sign_extend_16;
 
 	case BFD_RELOC_HI16_S:
 	  if (fixP->fx_pcrel)
@@ -6377,38 +6379,33 @@
 	case BFD_RELOC_HI16_S_PCREL:
 	case BFD_RELOC_PPC_VLE_HA16A:
 	case BFD_RELOC_PPC_VLE_HA16D:
-	  fieldval = SEX16 (PPC_HA (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HA (value);
+	  goto sign_extend_16;
 
 #ifdef OBJ_ELF
 	case BFD_RELOC_PPC64_HIGHER:
 	  if (fixP->fx_pcrel)
 	    goto bad_pcrel;
-	  fieldval = SEX16 (PPC_HIGHER (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HIGHER (value);
+	  goto sign_extend_16;
 
 	case BFD_RELOC_PPC64_HIGHER_S:
 	  if (fixP->fx_pcrel)
 	    goto bad_pcrel;
-	  fieldval = SEX16 (PPC_HIGHERA (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HIGHERA (value);
+	  goto sign_extend_16;
 
 	case BFD_RELOC_PPC64_HIGHEST:
 	  if (fixP->fx_pcrel)
 	    goto bad_pcrel;
-	  fieldval = SEX16 (PPC_HIGHEST (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HIGHEST (value);
+	  goto sign_extend_16;
 
 	case BFD_RELOC_PPC64_HIGHEST_S:
 	  if (fixP->fx_pcrel)
 	    goto bad_pcrel;
-	  fieldval = SEX16 (PPC_HIGHESTA (value));
-	  fixP->fx_no_overflow = 1;
-	  break;
+	  fieldval = PPC_HIGHESTA (value);
+	  goto sign_extend_16;
 
 	  /* The following relocs can't be calculated by the assembler.
 	     Leave the field zero.  */
