Implementing a new disassembly plugin

Radare2 has modular architecture, thus adding support for a new architecture is very easy, if you are fluent in C. For various reasons it might be easier to implement it out of the tree. For this we will need to create single C file, called asm_mycpu.c and makefile for it.

The key thing of RAsm plugin is a structure

  1. RAsmPlugin r_asm_plugin_mycpu = {
  2. .name = "mycpu",
  3. .license = "LGPL3",
  4. .desc = "MYCPU disassembly plugin",
  5. .arch = "mycpu",
  6. .bits = 32,
  7. .endian = R_SYS_ENDIAN_LITTLE,
  8. .disassemble = &disassemble
  9. };

where .disassemble is a pointer to disassembly function, which accepts the bytes buffer and length:

  1. static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len)

Makefile

  1. NAME=asm_snes
  2. R2_PLUGIN_PATH=$(shell r2 -H R2_USER_PLUGINS)
  3. LIBEXT=$(shell r2 -H LIBEXT)
  4. CFLAGS=-g -fPIC $(shell pkg-config --cflags r_anal)
  5. LDFLAGS=-shared $(shell pkg-config --libs r_anal)
  6. OBJS=$(NAME).o
  7. LIB=$(NAME).$(LIBEXT)
  8. all: $(LIB)
  9. clean:
  10. rm -f $(LIB) $(OBJS)
  11. $(LIB): $(OBJS)
  12. $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $(LIB)
  13. install:
  14. cp -f asm_mycpu.$(SO_EXT) $(R2_PLUGIN_PATH)
  15. uninstall:
  16. rm -f $(R2_PLUGIN_PATH)/asm_mycpu.$(SO_EXT)

asm_mycpu.c

  1. /* radare - LGPL - Copyright 2018 - user */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <r_types.h>
  5. #include <r_lib.h>
  6. #include <r_asm.h>
  7. static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
  8. struct op_cmd cmd = {
  9. .instr = "",
  10. .operands = ""
  11. };
  12. if (len < 2) return -1;
  13. int ret = decode_opcode (buf, len, &cmd);
  14. if (ret > 0) {
  15. snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s",
  16. cmd.instr, cmd.operands);
  17. }
  18. return op->size = ret;
  19. }
  20. RAsmPlugin r_asm_plugin_mycpu = {
  21. .name = "mycpu",
  22. .license = "LGPL3",
  23. .desc = "MYCPU disassembly plugin",
  24. .arch = "mycpu",
  25. .bits = 32,
  26. .endian = R_SYS_ENDIAN_LITTLE,
  27. .disassemble = &disassemble
  28. };
  29. #ifndef R2_PLUGIN_INCORE
  30. R_API RLibStruct radare_plugin = {
  31. .type = R_LIB_TYPE_ASM,
  32. .data = &r_asm_plugin_mycpu,
  33. .version = R2_VERSION
  34. };
  35. #endif

After compiling radare2 will list this plugin in the output:

  1. _d__ _8_32 mycpu LGPL3 MYCPU

Moving plugin into the tree

Pushing a new architecture into the main branch of r2 requires to modify several files in order to make it fit into the way the rest of plugins are built.

List of affected files:

  • plugins.def.cfg : add the asm.mycpu plugin name string in there
  • libr/asm/p/mycpu.mk : build instructions
  • libr/asm/p/asm_mycpu.c : implementation
  • libr/include/r_asm.h : add the struct definition in there

Check out how the NIOS II CPU disassembly plugin was implemented by reading those commits:

Implement RAsm plugin: https://github.com/radareorg/radare2/commit/933dc0ef6ddfe44c88bbb261165bf8f8b531476b

Implement RAnal plugin: https://github.com/radareorg/radare2/commit/ad430f0d52fbe933e0830c49ee607e9b0e4ac8f2