From 2db662bee41b0bf700e4783c89e9c1a8e74f782d Mon Sep 17 00:00:00 2001
From: Bob Wilson <bob.wilson@acm.org>
Date: Fri, 15 Jul 2005 20:43:37 +0000
Subject: [PATCH]         * elf32-xtensa.c (vsprint_msg): Add prototype.       
  (build_encoding_error_message): Delete.  Code moved into....        
 (elf_xtensa_do_reloc): ....here, and changed to give better         error
 messages for out of range literals.

---
 bfd/ChangeLog      |  7 ++++++
 bfd/elf32-xtensa.c | 55 ++++++++++++++++++++++------------------------
 2 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 7d836d1e3df..5c2eb816087 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,10 @@
+2005-07-15  Bob Wilson  <bob.wilson@acm.org>
+
+	* elf32-xtensa.c (vsprint_msg): Add prototype.
+	(build_encoding_error_message): Delete.  Code moved into....
+	(elf_xtensa_do_reloc): ....here, and changed to give better
+	error messages for out of range literals.
+
 2005-07-15  Alan Modra  <amodra@bigpond.net.au>
 
 	* elflink.c (bfd_elf_record_link_assignment): Remove output_bfd param.
diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
index b6ab7de9a34..79508891d27 100644
--- a/bfd/elf32-xtensa.c
+++ b/bfd/elf32-xtensa.c
@@ -36,7 +36,7 @@
 /* Local helper functions.  */
 
 static bfd_boolean add_extra_plt_sections (bfd *, int);
-static char *build_encoding_error_message (xtensa_opcode, bfd_vma);
+static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
 static bfd_reloc_status_type bfd_elf_xtensa_reloc
   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
 static bfd_boolean do_fix_for_relocatable_link
@@ -1742,7 +1742,30 @@ elf_xtensa_do_reloc (reloc_howto_type *howto,
       || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
 				   sbuff, newval))
     {
-      *error_message = build_encoding_error_message (opcode, relocation);
+      const char *opname = xtensa_opcode_name (isa, opcode);
+      const char *msg;
+
+      msg = "cannot encode";
+      if (is_direct_call_opcode (opcode))
+	{
+	  if ((relocation & 0x3) != 0)
+	    msg = "misaligned call target";
+	  else
+	    msg = "call target out of range";
+	}
+      else if (opcode == get_l32r_opcode ())
+	{
+	  if ((relocation & 0x3) != 0)
+	    msg = "misaligned literal target";
+	  else if (is_alt_relocation (howto->type))
+	    msg = "literal target out of range (too many literals)";
+	  else if (self_address > relocation)
+	    msg = "literal target out of range (try using text-section-literals)";
+	  else
+	    msg = "literal placed after use";
+	}
+
+      *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
       return bfd_reloc_dangerous;
     }
 
@@ -1767,7 +1790,7 @@ elf_xtensa_do_reloc (reloc_howto_type *howto,
 }
 
 
-static char * ATTRIBUTE_PRINTF(2,4)
+static char *
 vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
 {
   /* To reduce the size of the memory leak,
@@ -1797,32 +1820,6 @@ vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
 }
 
 
-static char *
-build_encoding_error_message (xtensa_opcode opcode, bfd_vma target_address)
-{
-  const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
-  const char *msg;
-
-  msg = "cannot encode";
-  if (is_direct_call_opcode (opcode))
-    {
-      if ((target_address & 0x3) != 0)
-	msg = "misaligned call target";
-      else
-	msg = "call target out of range";
-    }
-  else if (opcode == get_l32r_opcode ())
-    {
-      if ((target_address & 0x3) != 0)
-	msg = "misaligned literal target";
-      else
-	msg = "literal target out of range";
-    }
-
-  return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
-}
-
-
 /* This function is registered as the "special_function" in the
    Xtensa howto for handling simplify operations.
    bfd_perform_relocation / bfd_install_relocation use it to
-- 
GitLab