xtensa_insnbuf_from_chars buffer overflow checking

Message ID aum7neb5qQ87r3z9@squeak.grove.modra.org
State New
Headers
Series xtensa_insnbuf_from_chars buffer overflow checking |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Alan Modra Nov. 3, 2026, 10:46 a.m. UTC
  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, using fr_fix is wrong, and the assertions there will trigger.

The gdb changes are fairly obvious.  Just 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).

OK to apply?

include/
	* xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype.
bfd/
	* xtensa-isa.c (xtensa_insnbuf_from_chars): Return false on
	num_chars too small.
	* 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: Include stdbool.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.
  

Comments

Max Filippov Nov. 3, 2026, 1:22 p.m. UTC | #1
Hi Alan,

On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
>
> 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, using fr_fix is wrong, and the assertions there will trigger.
>
> The gdb changes are fairly obvious.  Just 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).
>
> OK to apply?
>
> include/
>         * xtensa-isa.h (xtensa_insnbuf_from_chars): Update prototype.
> bfd/
>         * xtensa-isa.c (xtensa_insnbuf_from_chars): Return false on
>         num_chars too small.
>         * 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: Include stdbool.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 --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c
> index e642ce03ac3..6febf68eca3 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,23 @@ 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);
> +  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> +  gas_assert (ret);

This assertion is triggered when assembling the attached source with
option --text-section-literals with the following backtrace:

#0  0x00007ffff7e062e0 in exit () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x000055555566b9ac in xexit ()
#2  0x00005555555a2c86 in as_abort ()
#3  0x00005555555b917f in xtensa_insnbuf_from_frag.isra ()
#4  0x00005555555b956f in frag_format_size ()
#5  0x00005555555bc4fe in get_aligned_diff ()
#6  0x00005555555bc8e1 in relax_frag_for_align ()
#7  0x00005555555c8a6a in xtensa_relax_frag ()
#8  0x00005555555b52f8 in relax_segment ()
#9  0x00005555555b5762 in relax_seg ()
#10 0x00005555555dbcdf in bfd_map_over_sections ()
#11 0x00005555555b589a in write_object_file ()
#12 0x000055555558e071 in main ()

...

> diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> index 5c25f122d29..4818559ff94 100644
> --- a/include/xtensa-isa.h
> +++ b/include/xtensa-isa.h
> @@ -210,7 +210,7 @@ extern int
>  xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
>                          unsigned char *cp, int num_chars);
>
> -extern void
> +extern bool

Other similar functions in this library have return type `int` and return
0 on success and a negative error code on failure. Could this function
please follow this pattern too? That's because this header is included
into the generated configuration-specific xtensa-modules.c and does
not include <stdbool.h> itself.

>  xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn,
>                            const unsigned char *cp, int num_chars);
>
...
  
Alan Modra Nov. 3, 2026, 11:04 p.m. UTC | #2
On Tue, Nov 03, 2026 at 05:22:26AM -0800, Max Filippov wrote:
> On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
> > +/* 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);
> > +  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> > +  gas_assert (ret);
> 
> This assertion is triggered when assembling the attached source with
> option --text-section-literals with the following backtrace:

Wrong testcase attached?  I don't reproduce the assertion here.

> #0  0x00007ffff7e062e0 in exit () from /lib/x86_64-linux-gnu/libc.so.6
> #1  0x000055555566b9ac in xexit ()
> #2  0x00005555555a2c86 in as_abort ()
> #3  0x00005555555b917f in xtensa_insnbuf_from_frag.isra ()
> #4  0x00005555555b956f in frag_format_size ()
> #5  0x00005555555bc4fe in get_aligned_diff ()
> #6  0x00005555555bc8e1 in relax_frag_for_align ()
> #7  0x00005555555c8a6a in xtensa_relax_frag ()
> #8  0x00005555555b52f8 in relax_segment ()
> #9  0x00005555555b5762 in relax_seg ()
> #10 0x00005555555dbcdf in bfd_map_over_sections ()
> #11 0x00005555555b589a in write_object_file ()
> #12 0x000055555558e071 in main ()
> 
> ...
> 
> > diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> > index 5c25f122d29..4818559ff94 100644
> > --- a/include/xtensa-isa.h
> > +++ b/include/xtensa-isa.h
> > @@ -210,7 +210,7 @@ extern int
> >  xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
> >                          unsigned char *cp, int num_chars);
> >
> > -extern void
> > +extern bool
> 
> Other similar functions in this library have return type `int` and return
> 0 on success and a negative error code on failure. Could this function
> please follow this pattern too? That's because this header is included
> into the generated configuration-specific xtensa-modules.c and does
> not include <stdbool.h> itself.

I can do that for consistency, but I can't say I like the practice of
avoiding bool.  Especially for functions like xtensa_opcode_is_branch
which are used everywhere testing == 1 or > 0 except one place in
xtensa-dis.c.  (Maybe a bug?)

> >  xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn,
> >                            const unsigned char *cp, int num_chars);
> >
> ...
> 
> -- 
> Thanks.
> -- Max
  
Max Filippov Nov. 4, 2026, 6:38 a.m. UTC | #3
On Tue, Nov 3, 2026 at 3:04 PM Alan Modra <amodra@gmail.com> wrote:
> On Tue, Nov 03, 2026 at 05:22:26AM -0800, Max Filippov wrote:
> > On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
> > > +/* 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);
> > > +  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> > > +  gas_assert (ret);
> >
> > This assertion is triggered when assembling the attached source with
> > option --text-section-literals with the following backtrace:
>
> Wrong testcase attached?  I don't reproduce the assertion here.

Um, the test case is correct, but I run it with custom xtensa configuration,
the one built from the following overlay:
  https://github.com/jcmvbkbc/xtensa-toolchain-build/blob/master/overlays/original/DE_233L_FPU.tar.gz
with the xtensa-dynconfig generator:
  https://github.com/jcmvbkbc/xtensa-dynconfig/tree/original

In any case, I looked at what's happening and I see that
xtensa_insnbuf_from_frag() is called for a frag that contains
literals and  the instruction length decoding call inside the
xtensa_insnbuf_from_chars() returns an error.
I don't see a reason to assert that xtensa_insnbuf_from_chars()
always succeeds, the callers of xtensa_insnbuf_from_frag()
may need to check the return value.

> > > diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> > > index 5c25f122d29..4818559ff94 100644
> > > --- a/include/xtensa-isa.h
> > > +++ b/include/xtensa-isa.h
> > > @@ -210,7 +210,7 @@ extern int
> > >  xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
> > >                          unsigned char *cp, int num_chars);
> > >
> > > -extern void
> > > +extern bool
> >
> > Other similar functions in this library have return type `int` and return
> > 0 on success and a negative error code on failure. Could this function
> > please follow this pattern too? That's because this header is included
> > into the generated configuration-specific xtensa-modules.c and does
> > not include <stdbool.h> itself.
>
> I can do that for consistency, but I can't say I like the practice of
> avoiding bool.

Neither do I. Is there a reason not to add `#include <stdbool.h>`
to the include/xtensa-isa.h ?

>  Especially for functions like xtensa_opcode_is_branch
> which are used everywhere testing == 1 or > 0 except one place in
> xtensa-dis.c.  (Maybe a bug?)

It would be a bug if it was possible to get there with an invalid opcode,
but according to the code above it that sets and checks `valid_insn`
it seems impossible.
  
Alan Modra Nov. 9, 2026, 4:20 a.m. UTC | #4
On Tue, Nov 03, 2026 at 10:38:01PM -0800, Max Filippov wrote:
> On Tue, Nov 3, 2026 at 3:04 PM Alan Modra <amodra@gmail.com> wrote:
> > On Tue, Nov 03, 2026 at 05:22:26AM -0800, Max Filippov wrote:
> > > On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
> > > > +/* 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);
> > > > +  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> > > > +  gas_assert (ret);
> > >
> > > This assertion is triggered when assembling the attached source with
> > > option --text-section-literals with the following backtrace:
> >
> > Wrong testcase attached?  I don't reproduce the assertion here.
> 
> Um, the test case is correct, but I run it with custom xtensa configuration,
> the one built from the following overlay:
>   https://github.com/jcmvbkbc/xtensa-toolchain-build/blob/master/overlays/original/DE_233L_FPU.tar.gz
> with the xtensa-dynconfig generator:
>   https://github.com/jcmvbkbc/xtensa-dynconfig/tree/original
> 
> In any case, I looked at what's happening and I see that
> xtensa_insnbuf_from_frag() is called for a frag that contains
> literals and  the instruction length decoding call inside the
> xtensa_insnbuf_from_chars() returns an error.

Right, the one that said
      /* This should never happen when the byte stream contains a
	 valid instruction.  Just read the maximum number of bytes....  */
but now returns a failure because we don't have a valid insn with
a different xtensa-modules.c length_decoder.

> I don't see a reason to assert that xtensa_insnbuf_from_chars()
> always succeeds, the callers of xtensa_insnbuf_from_frag()
> may need to check the return value.

I'm fine with that.  The assertion was an over-abundance of caution,
only there because I was changing the code quite a lot.

> > > > diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> > > > index 5c25f122d29..4818559ff94 100644
> > > > --- a/include/xtensa-isa.h
> > > > +++ b/include/xtensa-isa.h
> > > > @@ -210,7 +210,7 @@ extern int
> > > >  xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
> > > >                          unsigned char *cp, int num_chars);
> > > >
> > > > -extern void
> > > > +extern bool
> > >
> > > Other similar functions in this library have return type `int` and return
> > > 0 on success and a negative error code on failure. Could this function
> > > please follow this pattern too? That's because this header is included
> > > into the generated configuration-specific xtensa-modules.c and does
> > > not include <stdbool.h> itself.
> >
> > I can do that for consistency, but I can't say I like the practice of
> > avoiding bool.
> 
> Neither do I. Is there a reason not to add `#include <stdbool.h>`
> to the include/xtensa-isa.h ?

You can do that if you like.  I added it in xtensa-modules.c so as to
not waste a minor amount of time processing stdbool.h twice, as bfd.h
includes stdbool.h so it is already picked up in other files that
include xtensa-isa.h.  Of course I hadn't found that you replace
xtensa-modules.c in some builds..

> >  Especially for functions like xtensa_opcode_is_branch
> > which are used everywhere testing == 1 or > 0 except one place in
> > xtensa-dis.c.  (Maybe a bug?)
> 
> It would be a bug if it was possible to get there with an invalid opcode,
> but according to the code above it that sets and checks `valid_insn`
> it seems impossible.

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.

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).

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 --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"
  
Max Filippov Nov. 9, 2026, 2:04 p.m. UTC | #5
On Sun, Nov 8, 2026 at 8:20 PM Alan Modra <amodra@gmail.com> wrote:
> 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.

Thank you Alan, this version looks good to me and passes
all tests that I have.
  

Patch

diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
index 709f81e5bda..793441bd862 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)
+      || (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)
+      || (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))
+    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))
+    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))
+    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))
+    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))
+    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))
+    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))
+    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))
+	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,8 @@  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))
+    return XTENSA_UNDEFINED;
   fmt = xtensa_format_decode (isa, insnbuf);
   if (fmt == XTENSA_UNDEFINED
       || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
@@ -8164,8 +8172,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))
+	    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..0d686beff3f 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
+bool
 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 false;
+
   /* 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 false;
 
-  if (num_chars == 0 || num_chars > insn_size)
+  if (num_chars < insn_size)
+    return false;
+  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 true;
 }
 
 
diff --git a/bfd/xtensa-modules.c b/bfd/xtensa-modules.c
index 79bc6d1413f..f3c16ccb1a2 100644
--- a/bfd/xtensa-modules.c
+++ b/bfd/xtensa-modules.c
@@ -19,7 +19,8 @@ 
    02110-1301, USA.  */
 
 #include "ansidecl.h"
-#include <xtensa-isa.h>
+#include <stdbool.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..6febf68eca3 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,23 @@  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);
+  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
+  gas_assert (ret);
+  return ret;
+}
 
 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 +2694,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 +4610,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 +4634,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 +5154,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 +5197,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 +5237,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 +5830,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 +5853,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 +8143,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 +8490,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 +8519,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 +8801,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 +8883,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 +8934,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 +8986,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 +9619,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 +9736,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 +10414,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 +10628,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 +10751,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 +10849,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 +12990,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 +13255,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 +13269,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..9aca4c10dc7 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))
+	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))
+	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))
+	{
+	  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))
+	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..4818559ff94 100644
--- a/include/xtensa-isa.h
+++ b/include/xtensa-isa.h
@@ -210,7 +210,7 @@  extern int
 xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
 			 unsigned char *cp, int num_chars);
 
-extern void
+extern bool
 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"