Skip to contentSkip to conversation

disas: Allow linking multiple disassemblers in a heterogeneous binary

RFC PATCH In review Philippe Mathieu-Daudé 3 participants 12 messages Jan 3, 2025 – Jan 6, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover disas: Allow linking multiple disassemblers in a heterogeneous binary

Hi,

This series is an experiment to link multiple disassembler in the same system binary. At least in my prototype I wanted to get 3: x86 host and microblaze/xtensa targets.

Instead of having meson.build select one pair of host+target, we let targets to be selected by Kconfig. By default each target keeps selecting its disassembler, so there is no change in current uses of 1 target per binary.

Unfortunately I couldn't generalize to user emulation because we don't use Kconfig there.

Philippe Mathieu-Daudé (6):
target: Only link capstone to targets requiring it target/hexagon: Add Kconfig file target/xtensa: Move xtensa-isa.c to common_ss[]
disas: Have each system target select its disassembler
Kconfig: Expose host CONFIG_CAPSTONE definition
disas: Select capstone targets by default

 meson.build               |  4 ++-
 Kconfig                   |  1 +
 Kconfig.host              |  3 ++
 disas/Kconfig             | 60 +++++++++++++++++++++++++++++++++++++++
 disas/meson.build         |  1 -
 target/Kconfig            |  1 +
 target/alpha/Kconfig      |  1 +
 target/arm/meson.build    |  1 +
 target/avr/Kconfig        |  1 +
 target/hexagon/Kconfig    |  3 ++
 target/hppa/Kconfig       |  1 +
 target/i386/Kconfig       |  1 +
 target/i386/meson.build   |  1 +
 target/loongarch/Kconfig  |  1 +
 target/m68k/Kconfig       |  1 +
 target/microblaze/Kconfig |  1 +
 target/mips/Kconfig       |  1 +
 target/openrisc/Kconfig   |  1 +
 target/ppc/Kconfig        |  1 +
 target/ppc/meson.build    |  1 +
 target/riscv/Kconfig      |  2 ++
 target/rx/Kconfig         |  1 +
 target/s390x/Kconfig      |  1 +
 target/s390x/meson.build  |  1 +
 target/sh4/Kconfig        |  1 +
 target/sparc/Kconfig      |  2 ++
 target/tricore/Kconfig    |  1 +
 target/xtensa/Kconfig     |  1 +
 target/xtensa/meson.build |  3 +-
 29 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 disas/Kconfig
 create mode 100644 target/hexagon/Kconfig
Signature · 1 line
2.47.1
#1 lore raw

RFC PATCH 1/6 target: Only link capstone to targets requiring it

No need to link capstone to targets which don't use it.

---
 disas/meson.build        | 1 -
 target/arm/meson.build   | 1 +
 target/i386/meson.build  | 1 +
 target/ppc/meson.build   | 1 +
 target/s390x/meson.build | 1 +
 5 files changed, 4 insertions(+), 1 deletion(-)
disas/meson.build +0 -1
index bbfa1197835..4e80adf36ae 100644
@@ -20,4 +20,3 @@ common_ss.add(when: 'CONFIG_TCG', if_true: files(
 ))
 common_ss.add(files('disas-common.c'))
 system_ss.add(files('disas-mon.c'))
-specific_ss.add(capstone)
target/arm/meson.build +1 -0
index 2e10464dbb6..3d64334657d 100644
@@ -1,4 +1,5 @@
 arm_ss = ss.source_set()
+arm_ss.add(capstone)
 arm_ss.add(files(
   'cpu.c',
   'debug_helper.c',
target/i386/meson.build +1 -0
index 2e9c472f49d..7d1363ec21f 100644
@@ -1,4 +1,5 @@
 i386_ss = ss.source_set()
+i386_ss.add(capstone)
 i386_ss.add(files(
   'cpu.c',
   'gdbstub.c',
target/ppc/meson.build +1 -0
index db3b7a0c33b..b5f509612ec 100644
@@ -1,4 +1,5 @@
 ppc_ss = ss.source_set()
+ppc_ss.add(capstone)
 ppc_ss.add(files(
   'cpu-models.c',
   'cpu.c',
target/s390x/meson.build +1 -0
index 3b34ae034cb..6e00793944e 100644
@@ -1,4 +1,5 @@
 s390x_ss = ss.source_set()
+s390x_ss.add(capstone)
 s390x_ss.add(files(
   'cpu.c',
   'cpu_features.c',
Signature · 1 line
2.47.1
#2 lore raw

Re: [RFC PATCH 1/6] target: Only link capstone to targets requiring it

Quoted 16 lines · Philippe Mathieu-Daudé

No need to link capstone to targets which don't use it.

---
  disas/meson.build        | 1 -
  target/arm/meson.build   | 1 +
  target/i386/meson.build  | 1 +
  target/ppc/meson.build   | 1 +
  target/s390x/meson.build | 1 +
  5 files changed, 4 insertions(+), 1 deletion(-)
disas/meson.build +0 -0
index bbfa1197835..4e80adf36ae 100644

disas/meson.build common_ss.add(when: 'CONFIG_TCG', if_true: files( · line 23

Quoted 4 lines
disas/meson.build +0 -1
@@ -20,4 +20,3 @@ common_ss.add(when: 'CONFIG_TCG', if_true: files(
  ))
  common_ss.add(files('disas-common.c'))
  system_ss.add(files('disas-mon.c'))
-specific_ss.add(capstone)

You need to think about which hosts require capstone as well, which is most of them. This will help almost never.

r~

#3 lore raw

Re: [RFC PATCH 1/6] target: Only link capstone to targets requiring it

Quoted 12 lines · Richard Henderson
Quoted 11 lines · Philippe Mathieu-Daudé

No need to link capstone to targets which don't use it.

---
  disas/meson.build        | 1 -
  target/arm/meson.build   | 1 +
  target/i386/meson.build  | 1 +
  target/ppc/meson.build   | 1 +
  target/s390x/meson.build | 1 +

  5 files changed, 4 insertions(+), 1 deletion(-)
Quoted 12 lines

disas/meson.build common_ss.add(when: 'CONFIG_TCG', if_true: files( · line 23

Quoted 9 lines
disas/meson.build +0 -1
index bbfa1197835..4e80adf36ae 100644
@@ -20,4 +20,3 @@ common_ss.add(when: 'CONFIG_TCG', if_true: files(
   ))
   common_ss.add(files('disas-common.c'))
   system_ss.add(files('disas-mon.c'))
-specific_ss.add(capstone)

You need to think about which hosts require capstone as well, which is most of them.  This will help almost never.

Right, good point.

#4 lore raw

RFC PATCH 2/6 target/hexagon: Add Kconfig file

In reply to Philippe Mathieu-Daudé

Keep the Hexagon target in line with others.

---
 target/Kconfig         | 1 +
 target/hexagon/Kconfig | 2 ++
 2 files changed, 3 insertions(+)
 create mode 100644 target/hexagon/Kconfig
target/Kconfig +1 -0
index d0c7b59d9c7..17cfb4648d2 100644
@@ -1,6 +1,7 @@
 source alpha/Kconfig
 source arm/Kconfig
 source avr/Kconfig
+source hexagon/Kconfig
 source hppa/Kconfig
 source i386/Kconfig
 source loongarch/Kconfig
target/hexagon/Kconfig new file +2 -0
new file mode 100644
index 00000000000..7e556f35063
@@ -0,0 +1,2 @@
+config HEXAGON
+    bool
Signature · 1 line
2.47.1
Reviewed-by on v1 2/6
#5 lore raw

Re: [RFC PATCH 2/6] target/hexagon: Add Kconfig file

Quoted 4 lines · Philippe Mathieu-Daudé

Keep the Hexagon target in line with others.

---

Quoted 25 lines
  target/Kconfig         | 1 +
  target/hexagon/Kconfig | 2 ++
  2 files changed, 3 insertions(+)

create mode 100644 target/hexagon/Kconfig

target/Kconfig +1 -0
index d0c7b59d9c7..17cfb4648d2 100644
@@ -1,6 +1,7 @@
  source alpha/Kconfig
  source arm/Kconfig
  source avr/Kconfig
+source hexagon/Kconfig
  source hppa/Kconfig
  source i386/Kconfig
  source loongarch/Kconfig
target/hexagon/Kconfig new file +2 -0
new file mode 100644
index 00000000000..7e556f35063
@@ -0,0 +1,2 @@
+config HEXAGON
+    bool
#6 lore raw

RFC PATCH 3/6 target/xtensa: Move xtensa-isa.c to common_ss[]

In reply to Philippe Mathieu-Daudé

The Xtensa disassembler (selected with CONFIG_XTENSA_DIS=y) uses methods defined in xtensa-isa.c, so this file has to be compiled when the disassembler is linked.

---
 target/xtensa/meson.build | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/xtensa/meson.build +2 -1
index f8d60101e3d..68ebfe2582d 100644
@@ -12,9 +12,10 @@ xtensa_ss.add(files(
   'op_helper.c',
   'translate.c',
   'win_helper.c',
-  'xtensa-isa.c',
 ))
 
+common_ss.add(when: 'CONFIG_XTENSA_DIS', if_true: files('xtensa-isa.c'))
+
 xtensa_system_ss = ss.source_set()
 xtensa_system_ss.add(files(
   'dbg_helper.c',
Signature · 1 line
2.47.1
Reviewed-by on v1 3/6
#7 lore raw

Re: [RFC PATCH 3/6] target/xtensa: Move xtensa-isa.c to common_ss[]

Quoted 15 lines · Philippe Mathieu-Daudé

The Xtensa disassembler (selected with CONFIG_XTENSA_DIS=y) uses methods defined in xtensa-isa.c, so this file has to be compiled when the disassembler is linked.

---
  target/xtensa/meson.build | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
target/xtensa/meson.build +0 -0
index f8d60101e3d..68ebfe2582d 100644
@@ -12,9 +12,10 @@ xtensa_ss.add(files(
    'op_helper.c',

target/xtensa/meson.build xtensa_ss.add(files( · line 21

Quoted 10 lines
target/xtensa/meson.build +2 -1
    'translate.c',
    'win_helper.c',
-  'xtensa-isa.c',
  ))
 
+common_ss.add(when: 'CONFIG_XTENSA_DIS', if_true: files('xtensa-isa.c'))
+
  xtensa_system_ss = ss.source_set()
  xtensa_system_ss.add(files(
    'dbg_helper.c',

r~

#8 lore raw

RFC PATCH 4/6 disas: Have each system target select its disassembler

In reply to Philippe Mathieu-Daudé

No change for host or user emulation.

For system emulation, each target explicitly selects its disassembler.

This change allows selecting more than two (host + target) disassemblers in a binary supporting multiple targets.

---
 meson.build               |  3 ++-
 Kconfig                   |  1 +
 disas/Kconfig             | 56 +++++++++++++++++++++++++++++++++++++++
 target/alpha/Kconfig      |  1 +
 target/avr/Kconfig        |  1 +
 target/hexagon/Kconfig    |  1 +
 target/hppa/Kconfig       |  1 +
 target/i386/Kconfig       |  1 +
 target/loongarch/Kconfig  |  1 +
 target/m68k/Kconfig       |  1 +
 target/microblaze/Kconfig |  1 +
 target/mips/Kconfig       |  1 +
 target/openrisc/Kconfig   |  1 +
 target/ppc/Kconfig        |  1 +
 target/riscv/Kconfig      |  2 ++
 target/rx/Kconfig         |  1 +
 target/s390x/Kconfig      |  1 +
 target/sh4/Kconfig        |  1 +
 target/sparc/Kconfig      |  2 ++
 target/tricore/Kconfig    |  1 +
 target/xtensa/Kconfig     |  1 +
 21 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 disas/Kconfig
meson.build +2 -1
index e62251c7ca3..a35ef579c28 100644
@@ -3319,7 +3319,8 @@ foreach target : target_dirs
   endif
 
   foreach k, v: disassemblers
-    if host_arch.startswith(k) or config_target['TARGET_BASE_ARCH'].startswith(k)
+    if host_arch.startswith(k) \
+        or (target.endswith('-user') and config_target['TARGET_BASE_ARCH'].startswith(k))
       foreach sym: v
         config_target += { sym: 'y' }
       endforeach
Kconfig +1 -0
index 63ca7f46df7..d937ad3b7fe 100644
@@ -1,6 +1,7 @@
 source Kconfig.host
 source backends/Kconfig
 source accel/Kconfig
+source disas/Kconfig
 source target/Kconfig
 source hw/Kconfig
 source semihosting/Kconfig
disas/Kconfig new file +56 -0

56 lines not on this page · raw message

target/alpha/Kconfig +1 -0
index 267222c05b8..1b9cc8077db 100644
@@ -1,2 +1,3 @@
 config ALPHA
     bool
+    select ALPHA_DIS
target/avr/Kconfig +1 -0
index 155592d3537..7a7580c65f7 100644
@@ -1,2 +1,3 @@
 config AVR
     bool
+    select AVR_DIS
target/hexagon/Kconfig +1 -0
index 7e556f35063..a96415fb2fa 100644
@@ -1,2 +1,3 @@
 config HEXAGON
     bool
+    select HEXAGON_DIS
target/hppa/Kconfig +1 -0
index 395a35d799c..e8fbdc02346 100644
@@ -1,2 +1,3 @@
 config HPPA
     bool
+    select HPPA_DIS
target/i386/Kconfig +1 -0
index 6b0feef0299..af603c7cd3a 100644
@@ -1,5 +1,6 @@
 config I386
     bool
+    select I386_DIS
     select APIC
     # kvm_arch_fixup_msi_route() needs to access PCIDevice
     select PCI if KVM
target/loongarch/Kconfig +1 -0
index 46b26b1a857..690404b4a5a 100644
@@ -1,2 +1,3 @@
 config LOONGARCH64
     bool
+    select LOONGARCH_DIS
target/m68k/Kconfig +1 -0
index 23aae24ebec..57cd896dd0e 100644
@@ -1,3 +1,4 @@
 config M68K
     bool
+    select M68K_DIS
     imply SEMIHOSTING if TCG
target/microblaze/Kconfig +1 -0
index e91d58d88f2..09525a8e3ff 100644
@@ -1,3 +1,4 @@
 config MICROBLAZE
     bool
+    select MICROBLAZE_DIS
     select DEVICE_TREE # needed by boot.c
target/mips/Kconfig +1 -0
index 876048b150c..74a220f410a 100644
@@ -1,5 +1,6 @@
 config MIPS
     bool
+    select MIPS_DIS
     imply SEMIHOSTING if TCG
 
 config MIPS64
target/openrisc/Kconfig +1 -0
index cd66c2e3b6c..7e3c092345c 100644
@@ -1,3 +1,4 @@
 config OPENRISC
     bool
+    select OPENRISC_DIS
     select DEVICE_TREE # needed by boot.c
target/ppc/Kconfig +1 -0
index 0283711673e..930337c4422 100644
@@ -1,5 +1,6 @@
 config PPC
     bool
+    select PPC_DIS
 
 config PPC64
     bool
target/riscv/Kconfig +2 -0
index 11bc09b4146..f5cf6185a88 100644
@@ -1,9 +1,11 @@
 config RISCV32
     bool
+    select RISCV_DIS
     select ARM_COMPATIBLE_SEMIHOSTING if TCG
     select DEVICE_TREE # needed by boot.c
 
 config RISCV64
     bool
+    select RISCV_DIS
     select ARM_COMPATIBLE_SEMIHOSTING if TCG
     select DEVICE_TREE # needed by boot.c
target/rx/Kconfig +1 -0
index aceb5ed28fe..c050315add7 100644
@@ -1,2 +1,3 @@
 config RX
     bool
+    select RX_DIS
target/s390x/Kconfig +1 -0
index 8a95f2bc3ff..51bb0afa446 100644
@@ -1,5 +1,6 @@
 config S390X
     bool
+    select S390_DIS
     select PCI
     select S390_FLIC
 
target/sh4/Kconfig +1 -0
index 93b92f1e480..6ecf3173361 100644
@@ -1,4 +1,5 @@
 config SH4
     bool
+    select SH4_DIS
     # needed for sh_intc_get_pending_vector
     select SH_INTC
target/sparc/Kconfig +2 -0
index 70cc0f3a210..bcb53d964f0 100644
@@ -1,5 +1,7 @@
 config SPARC
     bool
+    select SPARC_DIS
 
 config SPARC64
     bool
+    select SPARC_DIS
target/tricore/Kconfig +1 -0
index 93134093093..c7c5fa8b074 100644
@@ -1,2 +1,3 @@
 config TRICORE
     bool
+    select TRICORE_DIS
target/xtensa/Kconfig +1 -0
index e8c2598c4d9..d54e3523401 100644
@@ -1,3 +1,4 @@
 config XTENSA
     bool
     imply SEMIHOSTING if TCG
+    select XTENSA_DIS
Signature · 1 line
2.47.1
#9 lore raw

RFC PATCH 5/6 Kconfig: Expose host CONFIG_CAPSTONE definition

In reply to Philippe Mathieu-Daudé
---
 meson.build  | 1 +
 Kconfig.host | 3 +++
 2 files changed, 4 insertions(+)
meson.build +1 -0
index a35ef579c28..28f9120a781 100644
@@ -3224,6 +3224,7 @@ host_kconfig = \
   (have_ivshmem ? ['CONFIG_IVSHMEM=y'] : []) + \
   (opengl.found() ? ['CONFIG_OPENGL=y'] : []) + \
   (libcbor.found() ? ['CONFIG_LIBCBOR=y'] : []) + \
+  (capstone.found() ? ['CONFIG_CAPSTONE=y'] : []) + \
   (gnutls.found() ? ['CONFIG_GNUTLS=y'] : []) + \
   (x11.found() ? ['CONFIG_X11=y'] : []) + \
   (fdt.found() ? ['CONFIG_FDT=y'] : []) + \
Kconfig.host +3 -0
index 842cbe0d6c5..23b729db945 100644
@@ -61,3 +61,6 @@ config HV_BALLOON_POSSIBLE
 
 config HAVE_RUST
     bool
+
+config CAPSTONE
+    bool
Signature · 1 line
2.47.1
#10 lore raw

RFC PATCH 6/6 disas: Select capstone targets by default

In reply to Philippe Mathieu-Daudé

When capstone is available, we get ARM/X86/PPC/S390X disassemblers in common_ss[] for free.

---
 disas/Kconfig | 4 ++++
 1 file changed, 4 insertions(+)
disas/Kconfig +4 -0
index 1d2c397c953..8af8ecd5c81 100644
@@ -3,6 +3,7 @@ config ALPHA_DIS
 
 config ARM_DIS
     bool
+    default y if CAPSTONE
 
 config AVR_DIS
     bool
@@ -15,6 +16,7 @@ config HPPA_DIS
 
 config I386_DIS
     bool
+    default y if CAPSTONE
 
 config LOONGARCH_DIS
     bool
@@ -33,6 +35,7 @@ config OPENRISC_DIS
 
 config PPC_DIS
     bool
+    default y if CAPSTONE
 
 config RISCV_DIS
     bool
@@ -42,6 +45,7 @@ config RX_DIS
 
 config S390_DIS
     bool
+    default y if CAPSTONE
 
 config SH4_DIS
     bool
Signature · 1 line
2.47.1