[2/3] RISC-V: Use obfd for disassembly instead of abfd

Message ID 20241210192434.336880-3-m.pikula@partner.samsung.com
State New
Headers
Series RISC-V: Fix disassembly for dynamic libraries |

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

Marek Pikula Dec. 10, 2024, 7:16 p.m. UTC
  In case the disassembled ELF is built for a different subset of ISA
extensions than the target binary (abfd) the disassembly might be
different.

For example, a binary might be built for a general `rv64gc` target, but
a library that the binary loads might be built for `rv64gcv`. In that
case, when only abfd provides information to disassembler, the vector
instructions don't get disassembled correctly (and are shown as generic
`.insn`). With this change, the disassembler checks which object file is
in scope. Thus, it adjusts the output to the ISA subset present in
`Tag_RISCV_arch` from the object file in the current scope.

Signed-off-by: Marek Pikuła <m.pikula@partner.samsung.com>
---
 opcodes/riscv-dis.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
  

Patch

diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
index f9cf9c5eed0..f5c2329cd8b 100644
--- a/opcodes/riscv-dis.c
+++ b/opcodes/riscv-dis.c
@@ -1371,6 +1371,7 @@  print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
   int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
 			     struct disassemble_info *);
   const char *default_arch = "rv64gc";
+  bfd *obfd = info->get_obfd_for_addr_func(memaddr, info);
 
   if (info->disassembler_options != NULL)
     {
@@ -1381,12 +1382,12 @@  print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
   else if (riscv_gpr_names == NULL)
     set_default_riscv_dis_options ();
 
-  if (info->abfd && bfd_get_flavour (info->abfd) == bfd_target_elf_flavour)
+  if (obfd && bfd_get_flavour (obfd) == bfd_target_elf_flavour)
     {
-      const char *sec_name = get_elf_backend_data (info->abfd)->obj_attrs_section;
-      if (bfd_get_section_by_name (info->abfd, sec_name) != NULL)
+      const char *sec_name = get_elf_backend_data (obfd)->obj_attrs_section;
+      if (bfd_get_section_by_name (obfd, sec_name) != NULL)
 	{
-	  obj_attribute *attr = elf_known_obj_attributes_proc (info->abfd);
+	  obj_attribute *attr = elf_known_obj_attributes_proc (obfd);
 	  unsigned int Tag_a = Tag_RISCV_priv_spec;
 	  unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
 	  unsigned int Tag_c = Tag_RISCV_priv_spec_revision;