RISC-V: Print highest address (-1) on the disassembler
authorTsukasa OI <research_trasio@irq.a4lg.com>
Sat, 27 Aug 2022 00:11:01 +0000 (00:11 +0000)
committerNelson Chu <nelson@rivosinc.com>
Fri, 2 Sep 2022 06:03:28 +0000 (14:03 +0800)
This patch makes possible to print the highest address (-1) and the addresses
related to gp which value is -1.  This is particularly useful if the highest
address space is used for I/O registers and corresponding symbols are defined.
Besides, despite that it is very rare to have GP the highest address, it would
be nice because we enabled highest address printing on regular cases.

gas/ChangeLog:

* testsuite/gas/riscv/dis-addr-topaddr.s: New test for the top
address (-1) printing.
* testsuite/gas/riscv/dis-addr-topaddr-32.d: Likewise.
* testsuite/gas/riscv/dis-addr-topaddr-64.d: Likewise.
* testsuite/gas/riscv/dis-addr-topaddr-gp.s: New test for
GP-relative addressing when GP is the highest address (-1).
* testsuite/gas/riscv/dis-addr-topaddr-gp-32.d: Likewise.
* testsuite/gas/riscv/dis-addr-topaddr-gp-64.d: Likewise.

opcodes/ChangeLog:

* riscv-dis.c (struct riscv_private_data): Add `to_print_addr' to
enable printing the highest address.
(maybe_print_address): Utilize `to_print_addr'.
(riscv_disassemble_insn): Likewise.

gas/testsuite/gas/riscv/dis-addr-topaddr-32.d [new file with mode: 0644]
gas/testsuite/gas/riscv/dis-addr-topaddr-64.d [new file with mode: 0644]
gas/testsuite/gas/riscv/dis-addr-topaddr-gp-32.d [new file with mode: 0644]
gas/testsuite/gas/riscv/dis-addr-topaddr-gp-64.d [new file with mode: 0644]
gas/testsuite/gas/riscv/dis-addr-topaddr-gp.s [new file with mode: 0644]
gas/testsuite/gas/riscv/dis-addr-topaddr.s [new file with mode: 0644]
opcodes/riscv-dis.c

diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr-32.d b/gas/testsuite/gas/riscv/dis-addr-topaddr-32.d
new file mode 100644 (file)
index 0000000..87854cd
--- /dev/null
@@ -0,0 +1,11 @@
+#as: -march=rv32ic
+#source: dis-addr-topaddr.s
+#objdump: -d
+
+.*:     file format elf32-(little|big)riscv
+
+
+Disassembly of section .text:
+
+0+000 <target>:
+[      ]+[0-9a-f]+:[   ]+fff00283[     ]+lb[           ]+t0,-1\(zero\) # ffffffff <addr_top>
diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr-64.d b/gas/testsuite/gas/riscv/dis-addr-topaddr-64.d
new file mode 100644 (file)
index 0000000..38f67ef
--- /dev/null
@@ -0,0 +1,11 @@
+#as: -march=rv64ic -defsym rv64=1
+#source: dis-addr-topaddr.s
+#objdump: -d
+
+.*:     file format elf64-(little|big)riscv
+
+
+Disassembly of section .text:
+
+0+000 <target>:
+[      ]+[0-9a-f]+:[   ]+fff00283[     ]+lb[           ]+t0,-1\(zero\) # ffffffffffffffff <addr_top>
diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr-gp-32.d b/gas/testsuite/gas/riscv/dis-addr-topaddr-gp-32.d
new file mode 100644 (file)
index 0000000..875bfe7
--- /dev/null
@@ -0,0 +1,12 @@
+#as: -march=rv32i
+#source: dis-addr-topaddr-gp.s
+#objdump: -d
+
+.*:     file format elf32-(little|big)riscv
+
+
+Disassembly of section .text:
+
+0+000 <target>:
+[      ]+[0-9a-f]+:[   ]+0051a283[     ]+lw[           ]+t0,5\(gp\) # 4 <addr_rel_gp_pos>
+[      ]+[0-9a-f]+:[   ]+ffd1a303[     ]+lw[           ]+t1,-3\(gp\) # fffffffc <addr_rel_gp_neg>
diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr-gp-64.d b/gas/testsuite/gas/riscv/dis-addr-topaddr-gp-64.d
new file mode 100644 (file)
index 0000000..5ac4b52
--- /dev/null
@@ -0,0 +1,12 @@
+#as: -march=rv64i -defsym rv64=1
+#source: dis-addr-topaddr-gp.s
+#objdump: -d
+
+.*:     file format elf64-(little|big)riscv
+
+
+Disassembly of section .text:
+
+0+000 <target>:
+[      ]+[0-9a-f]+:[   ]+0051a283[     ]+lw[           ]+t0,5\(gp\) # 4 <addr_rel_gp_pos>
+[      ]+[0-9a-f]+:[   ]+ffd1a303[     ]+lw[           ]+t1,-3\(gp\) # fffffffffffffffc <addr_rel_gp_neg>
diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr-gp.s b/gas/testsuite/gas/riscv/dis-addr-topaddr-gp.s
new file mode 100644 (file)
index 0000000..6ba9fc7
--- /dev/null
@@ -0,0 +1,15 @@
+.ifdef rv64
+topbase = 0xffffffff00000000
+.else
+topbase = 0
+.endif
+
+.set __global_pointer$, topbase + 0xffffffff  # -1
+.set addr_rel_gp_pos,             0x00000004  # +4
+.set addr_rel_gp_neg,   topbase + 0xfffffffc  # -4
+
+target:
+       # Use addresses relative to gp
+       # (gp is the highest address)
+       lw      t0, +5(gp)
+       lw      t1, -3(gp)
diff --git a/gas/testsuite/gas/riscv/dis-addr-topaddr.s b/gas/testsuite/gas/riscv/dis-addr-topaddr.s
new file mode 100644 (file)
index 0000000..b66587f
--- /dev/null
@@ -0,0 +1,10 @@
+.ifdef rv64
+topbase = 0xffffffff00000000
+.else
+topbase = 0
+.endif
+
+.set addr_top, topbase + 0xffffffff  # -1
+
+target:
+       lb      t0, -1(zero)
index b3ca680e50643e354642955e1bb743d436ffe0fb..160cc40f865d35329c98eb18b8a68c9566f10df4 100644 (file)
@@ -52,6 +52,8 @@ struct riscv_private_data
   bfd_vma gp;
   bfd_vma print_addr;
   bfd_vma hi_addr[OP_MASK_RD + 1];
+  bool to_print_addr;
+  bool has_gp;
 };
 
 /* Used for mapping symbols.  */
@@ -177,12 +179,13 @@ maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
       pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
       pd->hi_addr[base_reg] = -1;
     }
-  else if (base_reg == X_GP && pd->gp != (bfd_vma)-1)
+  else if (base_reg == X_GP && pd->has_gp)
     pd->print_addr = pd->gp + offset;
   else if (base_reg == X_TP || base_reg == 0)
     pd->print_addr = offset;
   else
     return;  /* Don't print the address.  */
+  pd->to_print_addr = true;
 
   /* Sign-extend a 32-bit value to a 64-bit value.  */
   if (wide)
@@ -601,14 +604,19 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
       int i;
 
       pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
-      pd->gp = -1;
-      pd->print_addr = -1;
+      pd->gp = 0;
+      pd->print_addr = 0;
       for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
        pd->hi_addr[i] = -1;
+      pd->to_print_addr = false;
+      pd->has_gp = false;
 
       for (i = 0; i < info->symtab_size; i++)
        if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
-         pd->gp = bfd_asymbol_value (info->symtab[i]);
+         {
+           pd->gp = bfd_asymbol_value (info->symtab[i]);
+           pd->has_gp = true;
+         }
     }
   else
     pd = info->private_data;
@@ -668,13 +676,13 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
          print_insn_args (op->args, word, memaddr, info);
 
          /* Try to disassemble multi-instruction addressing sequences.  */
-         if (pd->print_addr != (bfd_vma)-1)
+         if (pd->to_print_addr)
            {
              info->target = pd->print_addr;
              (*info->fprintf_styled_func)
                (info->stream, dis_style_comment_start, " # ");
              (*info->print_address_func) (info->target, info);
-             pd->print_addr = -1;
+             pd->to_print_addr = false;
            }
 
          /* Finish filling out insn_info fields.  */