[PATCH v4] RISC-V: Fix abort when displaying data and partial instructions

Nelson Chu nelson@rivosinc.com
Wed Feb 26 15:13:35 GMT 2025


Committed, thanks.

Nelson

On Tue, Feb 25, 2025 at 2:19 PM Nelson Chu <nelson@rivosinc.com> wrote:

> From: Charlie Jenkins <charlie@rivosinc.com>
>
> v4 changes:
> 1. The %llx doesn't enough to display bytes over 8, so refer to what .insn
> did,
> use a for loop to dump.
> 2. Added the case which Alan mentioned in the previous thread in
> dis-data.s.
> 3. Merged all into one patch.
>
> If data is encountered that is not a power of two, dump all of the data
> with
> a .<N>byte directive.  The current largest support risc-v instruction
> length
> is 22, so the data over 22 bytes will be displayed by,
> .insn, 22, ... + .<N-22>byte.
>
> Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
> ---
>  gas/testsuite/gas/riscv/dis-data.d            | 134 ++++++++++++++++++
>  gas/testsuite/gas/riscv/dis-data.s            |  48 +++++++
>  .../gas/riscv/dis-partial-insn-word.d         |   2 +-
>  opcodes/riscv-dis.c                           |  26 ++--
>  4 files changed, 200 insertions(+), 10 deletions(-)
>  create mode 100644 gas/testsuite/gas/riscv/dis-data.d
>  create mode 100644 gas/testsuite/gas/riscv/dis-data.s
>
> diff --git a/gas/testsuite/gas/riscv/dis-data.d
> b/gas/testsuite/gas/riscv/dis-data.d
> new file mode 100644
> index 00000000000..4b876f07650
> --- /dev/null
> +++ b/gas/testsuite/gas/riscv/dis-data.d
> @@ -0,0 +1,134 @@
> +#as: -mno-arch-attr
> +#source: dis-data.s
> +#objdump: -D
> +
> +.*:[   ]+file format .*
> +
> +
> +Disassembly of section .text.byte:
> +
> +0+000 <.text.byte>:
> +[      ]+0:[   ]+7f[   ]+.byte[        ]+0x7f
> +
> +Disassembly of section .text.short:
> +
> +0+000 <.text.short>:
> +[      ]+0:[   ]+ef7f[         ]+.short[       ]+0xef7f
> +
> +Disassembly of section .text.3byte:
> +
> +0+000 <.text.3byte>:
> +[      ]+0:[   ]+03ef7f[       ]+.3byte[       ]+0x03ef7f
> +
> +Disassembly of section .text.word:
> +
> +0+000 <.text.word>:
> +[      ]+0:[   ]+0403ef7f[     ]+.word[        ]+0x0403ef7f
> +
> +Disassembly of section .text.5byte:
> +
> +0+000 <.text.5byte>:
> +[      ]+0:[   ]+050403ef7f[   ]+.5byte[       ]+0x050403ef7f
> +
> +Disassembly of section .text.6byte:
> +
> +0+000 <.text.6byte>:
> +[      ]+0:[   ]+06050403ef7f[         ]+.6byte[       ]+0x06050403ef7f
> +
> +Disassembly of section .text.7byte:
> +
> +0+000 <.text.7byte>:
> +[      ]+0:[   ]+0706050403ef7f[       ]+.7byte[       ]+0x0706050403ef7f
> +
> +Disassembly of section .text.dword:
> +
> +0+000 <.text.dword>:
> +[      ]+0:[   ]+080706050403ef7f[     ]+.dword[
>  ]+0x080706050403ef7f
> +
> +Disassembly of section .text.9byte:
> +
> +0+000 <.text.9byte>:
> +[      ]+0:[   ]+09080706050403ef7f[   ]+.9byte[
>  ]+0x09080706050403ef7f
> +
> +Disassembly of section .text.10byte:
> +
> +0+000 <.text.10byte>:
> +[      ]+0:[   ]+0a09080706050403ef7f[         ]+.10byte[
> ]+0x0a09080706050403ef7f
> +
> +Disassembly of section .text.11byte:
> +
> +0+000 <.text.11byte>:
> +[      ]+0:[   ]+0b0a09080706050403ef7f[       ]+.11byte[
> ]+0x0b0a09080706050403ef7f
> +
> +Disassembly of section .text.12byte:
> +
> +0+000 <.text.12byte>:
> +[      ]+0:[   ]+0c0b0a09080706050403ef7f[     ]+.12byte[
> ]+0x0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.13byte:
> +
> +0+000 <.text.13byte>:
> +[      ]+0:[   ]+0d0c0b0a09080706050403ef7f[   ]+.13byte[
> ]+0x0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.14byte:
> +
> +0+000 <.text.14byte>:
> +[      ]+0:[   ]+0e0d0c0b0a09080706050403ef7f[         ]+.14byte[
> ]+0x0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.15byte:
> +
> +0+000 <.text.15byte>:
> +[      ]+0:[   ]+0f0e0d0c0b0a09080706050403ef7f[       ]+.15byte[
> ]+0x0f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.16byte:
> +
> +0+000 <.text.16byte>:
> +[      ]+0:[   ]+100f0e0d0c0b0a09080706050403ef7f[     ]+.16byte[
> ]+0x100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.17byte:
> +
> +0+000 <.text.17byte>:
> +[      ]+0:[   ]+11100f0e0d0c0b0a09080706050403ef7f[   ]+.17byte[
> ]+0x11100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.18byte:
> +
> +0+000 <.text.18byte>:
> +[      ]+0:[   ]+1211100f0e0d0c0b0a09080706050403ef7f[
>  ]+.18byte[      ]+0x1211100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.19byte:
> +
> +0+000 <.text.19byte>:
> +[      ]+0:[   ]+131211100f0e0d0c0b0a09080706050403ef7f[
>  ]+.19byte[      ]+0x131211100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.20byte:
> +
> +0+000 <.text.20byte>:
> +[      ]+0:[   ]+14131211100f0e0d0c0b0a09080706050403ef7f[
>  ]+.20byte[      ]+0x14131211100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.21byte:
> +
> +0+000 <.text.21byte>:
> +[      ]+0:[   ]+1514131211100f0e0d0c0b0a09080706050403ef7f[
>  ]+.21byte[      ]+0x1514131211100f0e0d0c0b0a09080706050403ef7f
> +
> +Disassembly of section .text.22byte:
> +
> +0+000 <.text.22byte>:
> +[      ]+0:[   ]+ef7f 0403 0605 0807[  ]+.insn[        ]+22,
> 0x161514131211100f0e0d0c0b0a09080706050403ef7f
> +[      ]+8:[   ]+0a09 0c0b 0e0d 100f ?
> +[      ]+10:[  ]+1211 1413 1615 ?
> +
> +Disassembly of section .text.23byte:
> +
> +0+000 <.text.23byte>:
> +[      ]+0:[   ]+ef7f 0403 0605 0807[  ]+.insn[        ]+22,
> 0x161514131211100f0e0d0c0b0a09080706050403ef7f
> +[      ]+8:[   ]+0a09 0c0b 0e0d 100f ?
> +[      ]+10:[  ]+1211 1413 1615 ?
> +[      ]+16:[  ]+17[   ]+.byte[        ]+0x17
> +
> +Disassembly of section .text.24byte:
> +
> +0+000 <.text.24byte>:
> +[      ]+0:[   ]+ef7f 0403 0605 0807[  ]+.insn[        ]+22,
> 0x161514131211100f0e0d0c0b0a09080706050403ef7f
> +[      ]+8:[   ]+0a09 0c0b 0e0d 100f ?
> +[      ]+10:[  ]+1211 1413 1615 ?
> +[      ]+16:[  ]+1817[         ]+.short[       ]+0x1817
> diff --git a/gas/testsuite/gas/riscv/dis-data.s
> b/gas/testsuite/gas/riscv/dis-data.s
> new file mode 100644
> index 00000000000..4da0fa03dcb
> --- /dev/null
> +++ b/gas/testsuite/gas/riscv/dis-data.s
> @@ -0,0 +1,48 @@
> +.section .text.byte, "ax"
> +.byte 0x7f
> +.section .text.short, "ax"
> +.byte 0x7f,0xef
> +.section .text.3byte, "ax"
> +.byte 0x7f,0xef,3
> +.section .text.word, "ax"
> +.byte 0x7f,0xef,3,4
> +.section .text.5byte, "ax"
> +.byte 0x7f,0xef,3,4,5
> +.section .text.6byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6
> +.section .text.7byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7
> +.section .text.dword, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8
> +.section .text.9byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9
> +.section .text.10byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10
> +.section .text.11byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11
> +.section .text.12byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12
> +.section .text.13byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13
> +.section .text.14byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14
> +.section .text.15byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15
> +.section .text.16byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16
> +.section .text.17byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
> +.section .text.18byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
> +.section .text.19byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
> +.section .text.20byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
> +.section .text.21byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
> +.section .text.22byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
> +.section .text.23byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
> +.section .text.24byte, "ax"
> +.byte 0x7f,0xef,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
> diff --git a/gas/testsuite/gas/riscv/dis-partial-insn-word.d
> b/gas/testsuite/gas/riscv/dis-partial-insn-word.d
> index 2f52153075e..af30c5be3d5 100644
> --- a/gas/testsuite/gas/riscv/dis-partial-insn-word.d
> +++ b/gas/testsuite/gas/riscv/dis-partial-insn-word.d
> @@ -8,4 +8,4 @@
>  Disassembly of section .text:
>
>  0+000 <target>:
> -[      ]+0:[   ]+000013[       ]+.word[        ]+0x000013
> +[      ]+0:[   ]+000013[       ]+.3byte[       ]+0x000013
> diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
> index 84c6deef7b6..9a6349931e7 100644
> --- a/opcodes/riscv-dis.c
> +++ b/opcodes/riscv-dis.c
> @@ -1289,6 +1289,7 @@ riscv_disassemble_data (bfd_vma memaddr
> ATTRIBUTE_UNUSED,
>                         disassemble_info *info)
>  {
>    info->display_endian = info->endian;
> +  int i;
>
>    switch (info->bytes_per_chunk)
>      {
> @@ -1308,14 +1309,6 @@ riscv_disassemble_data (bfd_vma memaddr
> ATTRIBUTE_UNUSED,
>        (*info->fprintf_styled_func)
>         (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
>        break;
> -    case 3:
> -      info->bytes_per_line = 7;
> -      (*info->fprintf_styled_func)
> -       (info->stream, dis_style_assembler_directive, ".word");
> -      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
> -      (*info->fprintf_styled_func)
> -       (info->stream, dis_style_immediate, "0x%06x", (unsigned) data);
> -      break;
>      case 4:
>        info->bytes_per_line = 8;
>        (*info->fprintf_styled_func)
> @@ -1335,7 +1328,22 @@ riscv_disassemble_data (bfd_vma memaddr
> ATTRIBUTE_UNUSED,
>          (unsigned long long) data);
>        break;
>      default:
> -      abort ();
> +      /* Arbitrary data so just print the bits in the shape of an .<N>byte
> +        directive.  */
> +      info->bytes_per_line = info->bytes_per_chunk;
> +      (*info->fprintf_styled_func)
> +       (info->stream, dis_style_assembler_directive, ".%dbyte",
> info->bytes_per_chunk);
> +      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
> +      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
> "0x");
> +      for (i = info->bytes_per_line; i > 0;)
> +       {
> +         i--;
> +         data = bfd_get_bits (packet + i, 8, false);
> +         (*info->fprintf_styled_func)
> +           (info->stream, dis_style_immediate, "%02x",
> +             (unsigned) data);
> +       }
> +      break;
>      }
>    return info->bytes_per_chunk;
>  }
> --
> 2.39.3 (Apple Git-146)
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/binutils/attachments/20250226/47eb59e2/attachment-0001.htm>


More information about the Binutils mailing list