Skip to content

Commit

Permalink
Improve rasm2 plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Aug 4, 2018
1 parent 196e9b2 commit da701e3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
16 changes: 13 additions & 3 deletions plugins/dev-anal.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
## Implementing a new analysis plugin

While implem
After implementing disassembly plugin, you might have noticed that output
is far from being good - no proper highlighting, no reference lines
and so on. This is because radare2 requires every architecture plugin
to provide also analysis information about every opcode. At the moment
the implementation of disassembly and opcodes analysis is separated between
two modules - RAsm and RAnal. Thus we need to write an analysis plugin too.
The principle is very similar - you just need to create a C file and
corresponding Makefile.

**Makefile**

```makefile
NAME=anal_snes
R2_PLUGIN_PATH=$(shell r2 -hh|grep LIBR_PLUGINS|awk '{print $$2}')
R2_PLUGIN_PATH=$(shell r2 -H|grep USER_PLUGINS|awk '{print $$2}')
CFLAGS=-g -fPIC $(shell pkg-config --cflags r_anal)
LDFLAGS=-shared $(shell pkg-config --libs r_anal)
OBJS=$(NAME).o
Expand All @@ -27,6 +36,7 @@ uninstall:
```

**anal_snes.c:**

```c
/* radare - LGPL - Copyright 2015 - condret */

Expand Down Expand Up @@ -77,7 +87,7 @@ struct r_lib_struct_t radare_plugin = {
```
After compiling radare2 will list this plugin in the output:
```
_d__ _8_16 snes LGPL3 SuperNES CPU
_dA_ _8_16 snes LGPL3 SuperNES CPU
```

**snes_op_table**.h: https://github.com/radare/radare2/blob/master/libr/asm/arch/snes/snes_op_table.h
Expand Down
81 changes: 81 additions & 0 deletions plugins/dev-asm.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,86 @@
## 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.

**Makefile**

```makefile
NAME=asm_snes
R2_PLUGIN_PATH=$(shell r2 -H|grep USER_PLUGINS|awk '{print $$2}')
CFLAGS=-g -fPIC $(shell pkg-config --cflags r_anal)
LDFLAGS=-shared $(shell pkg-config --libs r_anal)
OBJS=$(NAME).o
SO_EXT=$(shell uname|grep -q Darwin && echo dylib || echo so)
LIB=$(NAME).$(SO_EXT)

all: $(LIB)

clean:
rm -f $(LIB) $(OBJS)

$(LIB): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $(LIB)

install:
cp -f asm_mycpu.$(SO_EXT) $(R2_PLUGIN_PATH)

uninstall:
rm -f $(R2_PLUGIN_PATH)/asm_mycpu.$(SO_EXT)
```

**asm_mycpu.c**

```c
/* radare - LGPL - Copyright 2018 - user */

#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_asm.h>

static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
struct op_cmd cmd = {
.instr = "",
.operands = ""
};
if (len < 2) return -1;
int ret = decode_opcode (buf, len, &cmd);
if (ret > 0) {
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s %s",
cmd.instr, cmd.operands);
}
return op->size = ret;
}

RAsmPlugin r_asm_plugin_mycpu = {
.name = "mycpu",
.license = "LGPL3",
.desc = "MYCPU disassembly plugin",
.arch = "mycpu",
.bits = 32,
.endian = R_SYS_ENDIAN_LITTLE,
.disassemble = &disassemble
};

#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_mycpu,
.version = R2_VERSION
};
#endif
```

After compiling radare2 will list this plugin in the output:
```
_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:
Expand Down

0 comments on commit da701e3

Please sign in to comment.