hexrays_sample11.cpp
/*
* Hex-Rays Decompiler project
* Copyright (c) 2007-2024 by Hex-Rays, support@hex-rays.com
* ALL RIGHTS RESERVED.
*
* Sample plugin for Hex-Rays Decompiler.
* It installs a custom block optimization rule:
*
* goto L1 => goto L2
* ...
* L1:
* goto L2
*
* In other words we fix a goto target if it points to a chain of gotos.
* This improves the decompiler output in some cases.
*/
#include <hexrays.hpp>
//--------------------------------------------------------------------------
{
{
if ( handle_goto_chain(blk) )
return 1;
return 0;
}
//lint -e{818} ins could be made const
{
return false;
intvec_t visited;
int i = t0;
// follow the goto chain
while ( true )
{
if ( !visited.add_unique(i) )
return false; // an endless loop, prefer to keep things as is
// skip assertion instructions and find first regular instruction
break; // not a goto
i = m2->l.b;
}
if ( i == t0 )
return false; // not a chain
// all ok, found a goto chain
mgoto->l.b = i; // jump directly to the end of the chain
// fix the successor/predecessor lists
blk->succset[0] = i;
// since we changed the control flow graph, invalidate the use/def chains.
// stricly speaking it is not really necessary in our plugin because
// we did not move around any microcode operands.
mba->mark_chains_dirty();
// it is a good idea to verify microcode after each change
// however, it may be time consuming, so comment it out eventually
return true;
}
};
//--------------------------------------------------------------------------
struct plugin_ctx_t : public plugmod_t
{
goto_optimizer_t goto_optimizer;
plugin_ctx_t()
{
install_optblock_handler(&goto_optimizer);
}
~plugin_ctx_t()
{
remove_optblock_handler(&goto_optimizer);
}
virtual bool idaapi run(size_t) override;
};
//--------------------------------------------------------------------------
static plugmod_t *idaapi init()
{
return nullptr; // no decompiler
msg("Hex-rays version %s has been detected, %s ready to use\n",
hxver, PLUGIN.wanted_name);
return new plugin_ctx_t;
}
//--------------------------------------------------------------------------
bool idaapi plugin_ctx_t::run(size_t arg)
{
if ( arg == 1 )
{
remove_optblock_handler(&goto_optimizer);
msg("%s disabled\n", PLUGIN.wanted_name);
}
else if ( arg == 2 )
{
install_optblock_handler(&goto_optimizer);
msg("%s enabled\n", PLUGIN.wanted_name);
}
else
{
msg("The %d arg is unknown (1 disable, 2 enable)\n", int(arg));
}
return false;
}
//--------------------------------------------------------------------------
static const char comment[] = "Sample11 plugin for Hex-Rays decompiler";
//--------------------------------------------------------------------------
//
// PLUGIN DESCRIPTION BLOCK
//
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_MULTI, // The plugin can work with multiple idbs in parallel
init, // initialize
nullptr,
nullptr,
comment, // long comment about the plugin
nullptr, // multiline help about the plugin
"Optimize goto chains", // the preferred short name of the plugin
nullptr, // the preferred hotkey to run the plugin
};
const mblock_t * get_mblock(int n) const
Get basic block by its serial number.
Definition: hexrays.hpp:4856
intvec_t succset
control flow graph: list of our successors use nsucc() and succ() to access it
Definition: hexrays.hpp:3861
intvec_t predset
control flow graph: list of our predecessors use npred() and pred() to access it
Definition: hexrays.hpp:3859
HexRays SDK header file.
bool init_hexrays_plugin(int flags=0)
Check that your plugin is compatible with hex-rays decompiler.
Definition: hexrays.hpp:8601
const minsn_t * getf_reginsn(const minsn_t *ins)
Skip assertions forward.
Definition: hexrays.hpp:11047
bool remove_optblock_handler(optblock_t *opt)
Remove a block level custom optimizer.
Definition: hexrays.hpp:10496
void install_optblock_handler(optblock_t *opt)
Install a block level custom optimizer.
Definition: hexrays.hpp:10490