RISC-V: Do not go beyond a label when disassembling data

Message ID LO4P265MB5914F23AC24D70E2BAB89F1680FF2@LO4P265MB5914.GBRP265.PROD.OUTLOOK.COM
State New
Headers
Series RISC-V: Do not go beyond a label when disassembling data |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Joseph Faulls June 3, 2024, 9:42 a.m. UTC
  Calculating data length to disassemble defaults to 4, but stop_vma is
set to the next symbol so that the disassembler will not read beyond it.
This causes an "Address out of bounds" error if the next symbol is less
than 4 bytes away from the data to be disassembled.

opcodes/ChangeLog:

         * riscv-dis.c (riscv_data_length): Do not go beyond stop_vma.

gas/ChangeLog:

        * testsuite/gas/riscv/mapping-dis.d: Updated and added new testcase.
        * testsuite/gas/riscv/mapping-symbols.d: Likewise.
        * testsuite/gas/riscv/mapping.s: Added new testcase.
---
 gas/testsuite/gas/riscv/mapping-dis.d     | 9 +++++++++
 gas/testsuite/gas/riscv/mapping-symbols.d | 4 ++++
 gas/testsuite/gas/riscv/mapping.s         | 6 ++++++
 opcodes/riscv-dis.c                       | 4 +++-
 4 files changed, 22 insertions(+), 1 deletion(-)

--
2.34.1
  

Comments

Joseph Faulls June 12, 2024, 3:32 p.m. UTC | #1
Ping
  
Nelson Chu June 18, 2024, 3:56 a.m. UTC | #2
On Mon, Jun 3, 2024 at 5:42 PM Joseph Faulls <Joseph.Faulls@imgtec.com>
wrote:

> +.section .text.data.label
> +addi   a0, zero, 1
> +.short 3                       # $d, dumped as .short due to label
> +label:
> +.word 4
>

I expect the data should dump as .word before .short when porting the
mapping symbol from arm/aarch64 to risc-v.  Not sure if we should dump
data, which is placed in text, while considering labels.

Thanks
Nelson
  

Patch

diff --git a/gas/testsuite/gas/riscv/mapping-dis.d b/gas/testsuite/gas/riscv/mapping-dis.d
index b1a26fbd151..5c908d52ec6 100644
--- a/gas/testsuite/gas/riscv/mapping-dis.d
+++ b/gas/testsuite/gas/riscv/mapping-dis.d
@@ -26,6 +26,15 @@  Disassembly of section .text.data:
 [      ]+[0-9a-f]+:[   ]+4509[         ]+li[   ]+a0,2
 [      ]+[0-9a-f]+:[   ]+05000302[     ]+.word[        ]+0x05000302

+Disassembly of section .text.data.label:
+
+0+000 <label-0x4>:
+[      ]+[0-9a-f]+:[   ]+4505[         ]+li[   ]+a0,1
+[      ]+[0-9a-f]+:[   ]+0003[         ]+.short[       ]+0x0003
+
+0+004 <label>:
+[      ]+[0-9a-f]+:[   ]+00000004[     ]+.word[        ]+0x00000004
+
 Disassembly of section .text.odd.align.start.insn:

 0+000 <.text.odd.align.start.insn>:
diff --git a/gas/testsuite/gas/riscv/mapping-symbols.d b/gas/testsuite/gas/riscv/mapping-symbols.d
index 6af825d8ad3..3cf8893ed0c 100644
--- a/gas/testsuite/gas/riscv/mapping-symbols.d
+++ b/gas/testsuite/gas/riscv/mapping-symbols.d
@@ -17,6 +17,10 @@  SYMBOL TABLE:
 0+00 l       .text.data        0+00 \$d
 0+08 l       .text.data        0+00 \$xrv32i2p1_c2p0
 0+0c l       .text.data        0+00 \$d
+0+00 l    d  .text.data.label  0+00 .text.data.label
+0+00 l       .text.data.label  0+00 \$xrv32i2p1_c2p0
+0+02 l       .text.data.label  0+00 \$d
+0+04 l       .text.data.label  0+00 label
 0+00 l    d  .text.odd.align.start.insn        0+00 .text.odd.align.start.insn
 0+00 l       .text.odd.align.start.insn        0+00 \$xrv32i2p1_c2p0
 0+02 l       .text.odd.align.start.insn        0+00 \$d
diff --git a/gas/testsuite/gas/riscv/mapping.s b/gas/testsuite/gas/riscv/mapping.s
index 3014a69e792..2e8f3166ef7 100644
--- a/gas/testsuite/gas/riscv/mapping.s
+++ b/gas/testsuite/gas/riscv/mapping.s
@@ -32,6 +32,12 @@  addi a0, zero, 2             # $x, but same as previous addi, so removed
 .byte  5
 .option pop

+.section .text.data.label
+addi   a0, zero, 1
+.short 3                       # $d, dumped as .short due to label
+label:
+.word 4
+
 .section .text.odd.align.start.insn, "ax"
 .option push
 .option norelax
diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
index e6596c47423..55d8baa5ce9 100644
--- a/opcodes/riscv-dis.c
+++ b/opcodes/riscv-dis.c
@@ -1169,7 +1169,9 @@  riscv_data_length (bfd_vma memaddr,
   bfd_vma length;
   bool found = false;

-  length = 4;
+  length = info->stop_vma - memaddr;
+  if (length > 4)
+    length = 4;
   if (info->symtab_size != 0
       && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
       && last_map_symbol >= 0)