10.4 Language bindings

All language bindings supported by radare to script some actions are implemented as hack plugins.

LUA is probably the cleaner implementation of a language binding for radare, i recommend you to read the source at 'src/plug/hack/lua.c'. Here's the structure to register the plugin:

int radare_plugin_type = PLUGIN_TYPE_HACK;
struct plugin_hack_t radare_plugin = {
        .name = "lua",
        .desc = "lua plugin",
        .callback = &lua_hack_cmd
};

The 'lua_hack_cmd' accepts a string as argument which is the argument given when calling the plugin from the radare shell:

[0x00000000]> H lua my-script.lua

If no arguments given, the plugin will loop in a prompt executing the lines given as lua statements.

The same happens with other language bindings like ruby, python or perl.

In the same directory where the plugins are installed, there's a "radare.py" or "radare.lua" which describes the API for that language.

The APIs in radare for language bindings are just wrappers for the basic 'r.cmd()' function handled by the core which is hooked to 'radare_cmd()'.

Here's a small part of radare.py to exemplify this:

def flag_get(name):
        return r.cmd("? %s"%name).split(" ")[0].strip()

def flag_set(name, addr=None):
        if addr == None:
                r.cmd("f %s"%name)
        else:
                r.cmd("f %s @ 0xx"%name, addr)

def analyze_opcode(addr=None):
        """
        Returns a hashtable containing the information of the analysis of the opcode in the current seek.
        This is: 'opcode', 'size', 'type', 'bytes', 'offset', 'ref', 'jump' and 'fail'
        """
        if addr == None:
                return __str.to_hash(r.cmd("ao"))
        return __str.to_hash(r.cmd("ao @ 0x%x"%addr))

The use of these functions is quite natural:

from radare import *

aop = analyze_opcode(flag_get("eip"))
if aop["type"] == "jump":
       print "Jumping to 0x%08x"%aop["jump"]

Read the 'scripting' chapter to get a deeper look on this topic.

The clearest example about how to implement a language binding for radare is done in Ruby. Read it at src/plug/hack/ruby.c