[binutils-gdb] xtensa_insnbuf_from_chars buffer overflow checking

Alan Modra amodra@sourceware.org
Mon Nov 9 20:57:28 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bb1d54e2554f86cd3740f847fa3b333a299ed7f6

commit bb1d54e2554f86cd3740f847fa3b333a299ed7f6
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Nov 9 10:27:15 2026 +1030

    xtensa_insnbuf_from_chars buffer overflow checking
    
    R_XTENSA_ASM_SIMPLIFY has size zero, thus it can appear right at the
    end of a section without failing bfd_reloc_offset_in_range.  Of course
    the reloc should never appear at the end of a section let alone in
    debug sections, but fuzzers do horrible things.
    
    For such an object we end up at xtensa_insnbuf_from_chars with num_chars
    zero (via get_expanded_call_opcode, elf_xtensa_do_asm_simplify,
    elf_xtensa_do_reloc, bfd_elf_xtensa_reloc, bfd_perform_relocation,
    bfd_generic_get_relocated_section_contents,
    bfd_simple_get_relocated_section_contents).  A value of zero is
    allowed, because gas and gdb uses of xtensa_insnbuf_from_chars pass a
    zero rather than the actual buffer size.  When the buffer size really
    is zero we get a buffer overflow.  A relatively simple fix for this
    problem would be to change gas and gdb to pass INT_MAX rather than
    zero for num_chars.  That would allow proper checks in elf32-xtensa.c,
    but I think we can do better than that.
    
    This patch modifies xtensa_insnbuf_from_chars to return whether the
    buffer was large enough, supplies actual buffer length args to the
    function where it is used in gas and gdb, and checks return status in
    most places.  I omitted the status check in a few places in gas where
    other functions called had their status ignored.
    
    The gas changes use a wrapper for xtensa_insnbuf_from_chars called
    xtensa_insnbuf_from_frag since data in all cases is being read out of
    gas frags, and passing a frag pointer allows access to the frag buffer
    length.  I'm reasonably confident that xtensa_insnbuf_from_frag isn't
    called from a frag under construction, so frag->fr_fix has been set to
    the fixed length of the frag.  I'm less certain that
    xtensa_insnbuf_from_frag won't want to read past that into the
    variable part of a frag, for example with rs_machine_dependent frags.
    If so, just using fr_fix is wrong.
    
    The gdb changes are fairly obvious.  Before each
    xtensa_insnbuf_from_chars call, data has been read from the target
    into ibuf at address ba of length bt-ba.  We step through ibuf using
    address ia at ia-ba, so the remaining ibuf length is bt-ba-(ia-ba).
    
    Revised:
    - xtensa_insnbuf_from_chars return type and comment
    - don't include stdbool.h in xtensa-modules.c
    - remove one assert in xtensa_insnbuf_from_frag.
    
    include/
            * xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype
            and comment.
    bfd/
            * xtensa-isa.c (xtensa_insnbuf_from_chars): Return
            XTENSA_UNDEFINED on num_chars too small and length_decode
            errors.
            * elf32-xtensa.c (elf_xtensa_do_reloc): Check return from
            xtensa_insnbuf_from_chars.
            (replace_tls_insn): Likewise.
            (get_relocation_opcode): Likewise.
            (insn_decode_len): Likewise.
            (insn_num_slots): Likewise.
            (insn_decode_opcode): Likewise.
            (narrow_instruction): Likewise.
            (widen_instruction): Likewise.
            (get_expanded_call_opcode): Likewise.
            (compute_ebb_proposed_actions): Likewise.
            * xtensa-modules.c: Don't use system header include brackets
            for xtensa-isa.h.
    opcodes/
            * xtensa-dis.c: Include xtensa-isa.h after bfd.h.
    gas/
            * config/tc-xtensa.c (xtensa_insnbuf_from_frag): New function.
            (get_opcode_from_frag): Rename from get_opcode_from_buf.  Replace
            buf param with frag and offset params.  Use xtensa_insnbuf_from_frag
            and check return.  Adjust all callers.
            (vinsn_from_frag): Rename similarly and replace char* param
            with a fragS*, moving the fr_opcode indirection from callers
            to here.  Use xtensa_insnbuf_from_frag.  Adjust callers.
            (tinsn_from_frag): Rename and adjust similarly.
            (frag_format_size): Use xtensa_insnbuf_from_frag and check return.
            (xg_apply_fix_value): Likwise.
            (md_pcrel_from): Likwise.
            (next_instrs_are_b_retw): Likwise.
            (unrelaxed_frag_min_insn_count): Likwise.
            (unrelaxed_frag_has_b_j): Likwise.
            (xg_is_relaxable_fixup): Likwise.
            (md_convert_frag): Likwise.
            (xtensa_find_unaligned_branch_targets): Likewise but ignore return.
            (xtensa_find_unaligned_loops): Likwise.
            (xtensa_fix_short_loop_frags): Likwise.
            (xtensa_relax_frag): Likwise.
    gdb/
            * xtensa-tdep.c (xtensa_scan_prologue): Pass buffer length
            to xtensa_insnbuf_from_chars and check return.
            (call0_ret, call0_analyze_prologue, execute_code): Likewise.

Diff:
---
 bfd/elf32-xtensa.c     |  60 +++++++++++++++++------------
 bfd/xtensa-isa.c       |  18 +++++----
 bfd/xtensa-modules.c   |   2 +-
 gas/config/tc-xtensa.c | 101 ++++++++++++++++++++++++++-----------------------
 gdb/xtensa-tdep.c      |  15 ++++++--
 include/xtensa-isa.h   |  11 +++---
 opcodes/xtensa-dis.c   |   2 +-
 7 files changed, 117 insertions(+), 92 deletions(-)

diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
index 709f81e5bda..1c7e779ef5e 100644
--- a/bfd/elf32-xtensa.c
+++ b/bfd/elf32-xtensa.c
@@ -2050,10 +2050,9 @@ elf_xtensa_do_reloc (reloc_howto_type *howto,
   if (input_size <= address)
     return bfd_reloc_outofrange;
   /* Read the instruction into a buffer and decode the opcode.  */
-  xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
-			     input_size - address);
-  fmt = xtensa_format_decode (isa, ibuff);
-  if (fmt == XTENSA_UNDEFINED)
+  if (xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
+				 input_size - address) < 0
+      || (fmt = xtensa_format_decode (isa, ibuff)) == XTENSA_UNDEFINED)
     {
       *error_message = "cannot decode instruction format";
       return bfd_reloc_dangerous;
@@ -2417,10 +2416,9 @@ replace_tls_insn (Elf_Internal_Rela *rel,
   input_size = bfd_get_section_limit (abfd, input_section);
 
   /* Read the instruction into a buffer and decode the opcode.  */
-  xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
-			     input_size - rel->r_offset);
-  fmt = xtensa_format_decode (isa, ibuff);
-  if (fmt == XTENSA_UNDEFINED)
+  if (xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
+				 input_size - rel->r_offset) < 0
+      || (fmt = xtensa_format_decode (isa, ibuff)) == XTENSA_UNDEFINED)
     {
       *error_message = "cannot decode instruction format";
       return false;
@@ -4123,8 +4121,9 @@ get_relocation_opcode (bfd *abfd,
     }
 
   /* Decode the instruction.  */
-  xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
-			     sec->size - irel->r_offset);
+  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
+				 sec->size - irel->r_offset) < 0)
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, ibuff);
   slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
   if (slot == XTENSA_UNDEFINED)
@@ -4220,8 +4219,9 @@ insn_decode_len (bfd_byte *contents,
 
   if (ibuff == NULL)
     ibuff = xtensa_insnbuf_alloc (isa);
-  xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
-			     content_len - offset);
+  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
+				 content_len - offset) < 0)
+    return 0;
   fmt = xtensa_format_decode (isa, ibuff);
   if (fmt == XTENSA_UNDEFINED)
     return 0;
@@ -4245,8 +4245,9 @@ insn_num_slots (bfd_byte *contents,
 
   if (ibuff == NULL)
     ibuff = xtensa_insnbuf_alloc (isa);
-  xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
-			     content_len - offset);
+  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
+				 content_len - offset) < 0)
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, ibuff);
   if (fmt == XTENSA_UNDEFINED)
     return XTENSA_UNDEFINED;
@@ -4277,8 +4278,9 @@ insn_decode_opcode (bfd_byte *contents,
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
-			     content_len - offset);
+  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
+				 content_len - offset) < 0)
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED)
     return XTENSA_UNDEFINED;
@@ -4592,8 +4594,9 @@ narrow_instruction (bfd_byte *contents,
 
   /* We will hand-code a few of these for a little while.
      These have all been specified in the assembler aleady.  */
-  xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
-			     content_length - offset);
+  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
+				 content_length - offset) < 0)
+    return false;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (xtensa_format_num_slots (isa, fmt) != 1)
     return false;
@@ -4761,8 +4764,9 @@ widen_instruction (bfd_byte *contents,
 
   /* We will hand-code a few of these for a little while.
      These have all been specified in the assembler aleady.  */
-  xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
-			     content_length - offset);
+  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
+				 content_length - offset) < 0)
+    return false;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (xtensa_format_num_slots (isa, fmt) != 1)
     return false;
@@ -4914,7 +4918,8 @@ get_expanded_call_opcode (bfd_byte *buf, int bufsize, bool *p_uses_l32r)
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
+  if (xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize) < 0)
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED
       || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
@@ -4946,7 +4951,9 @@ get_expanded_call_opcode (bfd_byte *buf, int bufsize, bool *p_uses_l32r)
 
       /* Check that the next instruction is also CONST16.  */
       offset += xtensa_format_length (isa, fmt);
-      xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
+      if (xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset,
+				     bufsize - offset) < 0)
+	return XTENSA_UNDEFINED;
       fmt = xtensa_format_decode (isa, insnbuf);
       if (fmt == XTENSA_UNDEFINED
 	  || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
@@ -4967,7 +4974,9 @@ get_expanded_call_opcode (bfd_byte *buf, int bufsize, bool *p_uses_l32r)
 
   /* Next instruction should be an CALLXn with operand 0 == regno.  */
   offset += xtensa_format_length (isa, fmt);
-  xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
+  if (xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset,
+				 bufsize - offset) < 0)
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED
       || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
@@ -8164,8 +8173,9 @@ compute_ebb_proposed_actions (ebb_constraint *ebb_table)
 
 	  if (offset + MIN_INSN_LENGTH > ebb->content_length)
 	    goto decode_error;
-	  xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
-				     ebb->content_length - offset);
+	  if (xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
+					 ebb->content_length - offset) < 0)
+	    goto decode_error;
 	  fmt = xtensa_format_decode (isa, insnbuf);
 	  if (fmt == XTENSA_UNDEFINED)
 	    goto decode_error;
diff --git a/bfd/xtensa-isa.c b/bfd/xtensa-isa.c
index 74a234ab343..771a97cfb59 100644
--- a/bfd/xtensa-isa.c
+++ b/bfd/xtensa-isa.c
@@ -182,7 +182,7 @@ xtensa_insnbuf_to_chars (xtensa_isa isa,
    xtensa_insnbuf_to_chars for a discussion of why this is complicated
    by endianness.  */
 
-void
+int
 xtensa_insnbuf_from_chars (xtensa_isa isa,
 			   xtensa_insnbuf insn,
 			   const unsigned char *cp,
@@ -193,16 +193,19 @@ xtensa_insnbuf_from_chars (xtensa_isa isa,
 
   max_size = xtensa_isa_maxlength (isa);
 
+  /* No insn is smaller than 2 bytes, and we must be able to read
+     at least one byte to decode the insn.  */
+  if (num_chars < 2)
+    return XTENSA_UNDEFINED;
+
   /* Decode the instruction length so we know how many bytes to read.  */
   insn_size = (intisa->length_decode_fn) (cp);
   if (insn_size == XTENSA_UNDEFINED)
-    {
-      /* This should never happen when the byte stream contains a
-	 valid instruction.  Just read the maximum number of bytes....  */
-      insn_size = max_size;
-    }
+    return XTENSA_UNDEFINED;
 
-  if (num_chars == 0 || num_chars > insn_size)
+  if (num_chars < insn_size)
+    return XTENSA_UNDEFINED;
+  if (num_chars > insn_size)
     num_chars = insn_size;
 
   if (intisa->is_big_endian)
@@ -226,6 +229,7 @@ xtensa_insnbuf_from_chars (xtensa_isa isa,
 
       insn[word_inx] |= (unsigned) (*cp & 0xff) << bit_inx;
     }
+  return 0;
 }
 
 

diff --git a/bfd/xtensa-modules.c b/bfd/xtensa-modules.c
index 79bc6d1413f..912e7e49eda 100644
--- a/bfd/xtensa-modules.c
+++ b/bfd/xtensa-modules.c
@@ -19,7 +19,7 @@
    02110-1301, USA.  */
 
 #include "ansidecl.h"
-#include <xtensa-isa.h>
+#include "xtensa-isa.h"
 #include "xtensa-isa-internal.h"
 
 

diff --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c
index e642ce03ac3..274f6eb1c4d 100644
--- a/gas/config/tc-xtensa.c
+++ b/gas/config/tc-xtensa.c
@@ -543,7 +543,7 @@ static bool tinsn_has_invalid_symbolic_operands (const TInsn *);
 static bool tinsn_has_complex_operands (const TInsn *);
 static bool tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
 static bool tinsn_check_arguments (const TInsn *);
-static void tinsn_from_chars (TInsn *, char *, int);
+static void tinsn_from_frag (TInsn *, const fragS *, int);
 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
 static int get_num_stack_text_bytes (IStack *);
 static int get_num_stack_literal_bytes (IStack *);
@@ -558,7 +558,7 @@ static bool vinsn_has_specific_opcodes (vliw_insn *);
 static void xg_free_vinsn (vliw_insn *);
 static bool vinsn_to_insnbuf
   (vliw_insn *, char *, fragS *, bool);
-static void vinsn_from_chars (vliw_insn *, char *);
+static void vinsn_from_frag (vliw_insn *, const fragS *);
 
 /* Expression Utilities.  */
 
@@ -2665,9 +2665,21 @@ get_relaxable_immed (xtensa_opcode opcode)
   return last_immed;
 }
 
+/* Wrapper for xtensa_insnbuf_from_chars to convert frag bytes
+   to xtensa_insnbuf.  */
+
+static bool
+xtensa_insnbuf_from_frag (xtensa_isa isa, xtensa_insnbuf insnbuf,
+			  const fragS *frag, size_t off)
+{
+  const unsigned char *buf = (const unsigned char *) frag->fr_literal + off;
+  size_t len = frag->fr_fix - off;
+  gas_assert (frag->fr_fix > off);
+  return xtensa_insnbuf_from_chars (isa, insnbuf, buf, len) >= 0;
+}
 
 static xtensa_opcode
-get_opcode_from_buf (const char *buf, int slot)
+get_opcode_from_frag (const fragS *frag, size_t off, int slot)
 {
   static xtensa_insnbuf insnbuf = NULL;
   static xtensa_insnbuf slotbuf = NULL;
@@ -2680,7 +2692,8 @@ get_opcode_from_buf (const char *buf, int slot)
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0);
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, frag, off))
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED)
     return XTENSA_UNDEFINED;
@@ -4595,7 +4608,7 @@ next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
   if (next_fragP == NULL)
     return false;
 
-  out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
+  out_opcode = get_opcode_from_frag (next_fragP, 0, 0);
   if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
     {
       *opcode = out_opcode;
@@ -4619,9 +4632,8 @@ frag_format_size (const fragS *fragP)
   if (fragP == NULL)
     return XTENSA_UNDEFINED;
 
-  xtensa_insnbuf_from_chars (isa, insnbuf,
-			     (unsigned char *) fragP->fr_literal, 0);
-
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, fragP, 0))
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED)
     return XTENSA_UNDEFINED;
@@ -5140,8 +5152,7 @@ xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
 	      addressT branch_align, frag_addr;
 	      xtensa_format fmt;
 
-	      xtensa_insnbuf_from_chars
-		(isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
+	      xtensa_insnbuf_from_frag (isa, insnbuf, frag, 0);
 	      fmt = xtensa_format_decode (isa, insnbuf);
 	      op_size = xtensa_format_length (isa, fmt);
 	      branch_align = 1 << branch_align_power (sec);
@@ -5184,8 +5195,7 @@ xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
 
 	      if (frag)
 		{
-		  xtensa_insnbuf_from_chars
-		    (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
+		  xtensa_insnbuf_from_frag (isa, insnbuf, frag, 0);
 		  fmt = xtensa_format_decode (isa, insnbuf);
 		  op_size = xtensa_format_length (isa, fmt);
 		  frag_addr = frag->fr_address % xtensa_fetch_width;
@@ -5225,7 +5235,8 @@ xg_apply_fix_value (fixS *fixP, valueT val)
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, fixP->fx_frag, fixP->fx_where))
+    as_fatal (_("undecodable fix"));
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED)
     as_fatal (_("undecodable fix"));
@@ -5817,7 +5828,6 @@ md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
 long
 md_pcrel_from (fixS *fixP)
 {
-  char *insn_p;
   static xtensa_insnbuf insnbuf = NULL;
   static xtensa_insnbuf slotbuf = NULL;
   int opnum;
@@ -5841,8 +5851,8 @@ md_pcrel_from (fixS *fixP)
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
-  xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0);
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, fixP->fx_frag, fixP->fx_where))
+    as_fatal (_("bad instruction format"));
   fmt = xtensa_format_decode (isa, insnbuf);
 
   if (fmt == XTENSA_UNDEFINED)
@@ -8131,7 +8141,7 @@ xtensa_mark_narrow_branches (void)
 	      {
 		vliw_insn vinsn;
 
-		vinsn_from_chars (&vinsn, fragP->fr_opcode);
+		vinsn_from_frag (&vinsn, fragP);
 		tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
 
 		if (vinsn.num_slots == 1
@@ -8478,8 +8488,8 @@ next_instrs_are_b_retw (fragS *fragP)
     return false;
 
   /* Check for the conditional branch.  */
-  xtensa_insnbuf_from_chars
-    (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, next_fragP, offset))
+    return false;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED)
     return false;
@@ -8507,8 +8517,8 @@ next_instrs_are_b_retw (fragS *fragP)
     return false;
 
   /* Check for the retw/retw.n.  */
-  xtensa_insnbuf_from_chars
-    (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, next_fragP, offset))
+    return false;
   fmt = xtensa_format_decode (isa, insnbuf);
 
   /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
@@ -8789,7 +8799,7 @@ xtensa_fix_short_loop_frags (void)
 	      {
 		TInsn t_insn;
 		fragS *loop_frag = next_non_empty_frag (fragP);
-		tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0);
+		tinsn_from_frag (&t_insn, loop_frag, 0);
 		current_opcode = t_insn.opcode;
 		gas_assert (xtensa_opcode_is_loop (xtensa_default_isa,
 					       current_opcode) == 1);
@@ -8871,11 +8881,8 @@ unrelaxed_frag_min_insn_count (fragS *fragP)
     {
       xtensa_format fmt;
 
-      xtensa_insnbuf_from_chars
-	(isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
-      fmt = xtensa_format_decode (isa, insnbuf);
-
-      if (fmt == XTENSA_UNDEFINED)
+      if (!xtensa_insnbuf_from_frag (isa, insnbuf, fragP, offset)
+	  || (fmt = xtensa_format_decode (isa, insnbuf)) == XTENSA_UNDEFINED)
 	{
 	  as_fatal (_("undecodable instruction in instruction frag"));
 	  return insn_count;
@@ -8925,16 +8932,15 @@ unrelaxed_frag_has_b_j (fragS *fragP)
       xtensa_format fmt;
       int slot;
 
-      xtensa_insnbuf_from_chars
-	(isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
+      if (!xtensa_insnbuf_from_frag (isa, insnbuf, fragP, offset))
+	return false;
       fmt = xtensa_format_decode (isa, insnbuf);
       if (fmt == XTENSA_UNDEFINED)
 	return false;
 
       for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
 	{
-	  xtensa_opcode opcode =
-	    get_opcode_from_buf (fragP->fr_literal + offset, slot);
+	  xtensa_opcode opcode = get_opcode_from_frag (fragP, offset, slot);
 	  if (xtensa_opcode_is_branch (isa, opcode) == 1
 	      || xtensa_opcode_is_jump (isa, opcode) == 1)
 	    return true;
@@ -8978,7 +8984,7 @@ xtensa_sanity_check (void)
 		  {
 		    if (!insnbuf)
 		      insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
-		    tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
+		    tinsn_from_frag (&t_insn, fragP, 0);
 		    tinsn_immed_from_frag (&t_insn, fragP, 0);
 
 		    if (xtensa_opcode_is_loop (xtensa_default_isa,
@@ -9611,9 +9617,8 @@ static bool xg_is_relaxable_fixup (fixS *fixP)
   if (vma_abs (delta) < J_RANGE - J_MARGIN)
     return false;
 
-  xtensa_insnbuf_from_chars (isa, trampoline_buf,
-			     (unsigned char *) fixP->fx_frag->fr_literal +
-			     fixP->fx_where, 0);
+  gas_assert (xtensa_insnbuf_from_frag (isa, trampoline_buf,
+					fixP->fx_frag, fixP->fx_where));
   fmt = xtensa_format_decode (isa, trampoline_buf);
   gas_assert (fmt != XTENSA_UNDEFINED);
   slot = fixP->tc_fix_data.slot;
@@ -9729,8 +9734,8 @@ xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
       if (vbuf == NULL)
 	vbuf = xtensa_insnbuf_alloc (isa);
 
-      xtensa_insnbuf_from_chars
-	(isa, vbuf, (unsigned char *) fragP->fr_opcode, 0);
+      xtensa_insnbuf_from_frag (isa, vbuf, fragP,
+				fragP->fr_opcode - fragP->fr_literal);
       fmt = xtensa_format_decode (isa, vbuf);
       num_slots = xtensa_format_num_slots (isa, fmt);
 
@@ -10407,7 +10412,7 @@ relax_frag_immed (segT segP,
   gas_assert (fragP->fr_opcode != NULL);
 
   xg_clear_vinsn (&cur_vinsn);
-  vinsn_from_chars (&cur_vinsn, fragP->fr_opcode);
+  vinsn_from_frag (&cur_vinsn, fragP);
   if (cur_vinsn.num_slots > 1)
     from_wide_insn = true;
 
@@ -10621,8 +10626,8 @@ md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
       if (vbuf == NULL)
 	vbuf = xtensa_insnbuf_alloc (isa);
 
-      xtensa_insnbuf_from_chars
-	(isa, vbuf, (unsigned char *) fragp->fr_opcode, 0);
+      xtensa_insnbuf_from_frag (isa, vbuf, fragp,
+				fragp->fr_opcode - fragp->fr_literal);
       fmt = xtensa_format_decode (isa, vbuf);
       num_slots = xtensa_format_num_slots (isa, fmt);
 
@@ -10744,7 +10749,7 @@ convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
   offsetT frag_offset;
 
   gas_assert (slot == 0);
-  tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
+  tinsn_from_frag (&tinsn, fragP, 0);
 
   if (fragP->tc_frag_data.is_aligning_branch == 1)
     {
@@ -10842,7 +10847,7 @@ convert_frag_immed (segT segP,
 
   xg_clear_vinsn (&cur_vinsn);
 
-  vinsn_from_chars (&cur_vinsn, fr_opcode);
+  vinsn_from_frag (&cur_vinsn, fragP);
   if (cur_vinsn.num_slots > 1)
     from_wide_insn = true;
 
@@ -12983,12 +12988,12 @@ tinsn_check_arguments (const TInsn *insn)
 /* Load an instruction from its encoded form.  */
 
 static void
-tinsn_from_chars (TInsn *tinsn, char *f, int slot)
+tinsn_from_frag (TInsn *tinsn, const fragS *frag, int slot)
 {
   vliw_insn vinsn;
 
   xg_init_vinsn (&vinsn);
-  vinsn_from_chars (&vinsn, f);
+  vinsn_from_frag (&vinsn, frag);
 
   *tinsn = vinsn.slots[slot];
   xg_free_vinsn (&vinsn);
@@ -13248,7 +13253,7 @@ vinsn_to_insnbuf (vliw_insn *vinsn,
 
 
 static void
-vinsn_from_chars (vliw_insn *vinsn, char *f)
+vinsn_from_frag (vliw_insn *vinsn, const fragS *frag)
 {
   static xtensa_insnbuf insnbuf = NULL;
   static xtensa_insnbuf slotbuf = NULL;
@@ -13262,9 +13267,9 @@ vinsn_from_chars (vliw_insn *vinsn, char *f)
       slotbuf = xtensa_insnbuf_alloc (isa);
     }
 
-  xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0);
-  fmt = xtensa_format_decode (isa, insnbuf);
-  if (fmt == XTENSA_UNDEFINED)
+  if (!xtensa_insnbuf_from_frag (isa, insnbuf, frag,
+				 frag->fr_opcode - frag->fr_literal)
+      || (fmt = xtensa_format_decode (isa, insnbuf)) == XTENSA_UNDEFINED)
     as_fatal (_("cannot decode instruction format"));
   vinsn->format = fmt;
   vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
diff --git a/gdb/xtensa-tdep.c b/gdb/xtensa-tdep.c
index 9af18a857ba..cad1f39855a 100644
--- a/gdb/xtensa-tdep.c
+++ b/gdb/xtensa-tdep.c
@@ -1134,7 +1134,8 @@ xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
 	    RETURN_FP;
 	}
 
-      xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
+      if (xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia - ba], bt - ia) < 0)
+	RETURN_FP;
       ifmt = xtensa_format_decode (isa, ins);
       if (ifmt == XTENSA_UNDEFINED)
 	RETURN_FP;
@@ -2010,7 +2011,8 @@ call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
 	    RETURN_RET;
 	}
 
-      xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
+      if (xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia - ba], bt - ia) < 0)
+	RETURN_RET;
       ifmt = xtensa_format_decode (isa, ins);
       if (ifmt == XTENSA_UNDEFINED)
 	RETURN_RET;
@@ -2415,7 +2417,11 @@ call0_analyze_prologue (struct gdbarch *gdbarch,
 
       /* Decode format information.  */
 
-      xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
+      if (xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia - ba], bt - ia) < 0)
+	{
+	  fail = 1;
+	  goto done;
+	}
       ifmt = xtensa_format_decode (isa, ins);
       if (ifmt == XTENSA_UNDEFINED)
 	{
@@ -2795,7 +2801,8 @@ execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
 	  if (target_read_memory (ba, ibuf, bt - ba) != 0)
 	    return xtNoExceptionHandler;
 	}
-      xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
+      if (xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia - ba], bt - ia) < 0)
+	return xtNoExceptionHandler;
       ifmt = xtensa_format_decode (isa, ins);
       if (ifmt == XTENSA_UNDEFINED)
 	return xtNoExceptionHandler;
diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
index 5c25f122d29..70563ea53ef 100644
--- a/include/xtensa-isa.h
+++ b/include/xtensa-isa.h
@@ -200,17 +200,16 @@ xtensa_insnbuf_free (xtensa_isa isa, xtensa_insnbuf buf);
    XTENSA_UNDEFINED on error.  The "from_chars" function first reads the
    minimal number of bytes required to decode the instruction length and
    then proceeds to copy the entire instruction into the buffer; if the
-   memory does not contain a valid instruction, it copies the maximum
-   number of bytes required for the longest Xtensa instruction.  The
-   "num_chars" argument may be used to limit the number of bytes that
-   can be read or written.  Otherwise, if "num_chars" is zero, the
-   functions may read or write past the end of the code.  */
+   memory does not contain a valid instruction it returns
+   XTENSA_UNDEFINED.  Zero is returned on success.  The "num_chars"
+   argument may be used to limit the number of bytes that can be read
+   or written.  */
 
 extern int
 xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
 			 unsigned char *cp, int num_chars);
 
-extern void
+extern int
 xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn,
 			   const unsigned char *cp, int num_chars);
 
diff --git a/opcodes/xtensa-dis.c b/opcodes/xtensa-dis.c
index 9b17e5e1f7a..ae29dc3cfe0 100644
--- a/opcodes/xtensa-dis.c
+++ b/opcodes/xtensa-dis.c
@@ -24,10 +24,10 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <string.h>
-#include "xtensa-isa.h"
 #include "ansidecl.h"
 #include "libiberty.h"
 #include "bfd.h"
+#include "xtensa-isa.h"
 #include "elf/xtensa.h"
 #include "disassemble.h"


More information about the Binutils-cvs mailing list