Hi Alan, On Tue, Nov 3, 2026 at 2:46 AM Alan Modra 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 itself. > xtensa_insnbuf_from_chars (xtensa_isa isa, xtensa_insnbuf insn, > const unsigned char *cp, int num_chars); > ... -- Thanks. -- Max